Reputation: 5
I've got the following line in my code, which works well, but it looks very ugly.
if not line.startswith("<ul>") and not line.startswith("<ol>") and not line.startswith("<li>"):
Is there a better way to write this line?
Thanks
Upvotes: 0
Views: 102
Reputation: 1
Needless to say there are multiple ways to do this (as with anything in coding). But if you're planning on using these tags later again, another way to do it would be to create a list that holds those tags, then reference that list whenever you need to. I.e.
tags = ["<ul>", "<ol>", "<li>"]
#your code here
if line.startswith not in tags:
#your code here
Upvotes: 0
Reputation: 5893
Use a regular expression
import re
if not re.match("^<ol>|^<ul>|^<li>", line):
Upvotes: 3
Reputation: 31905
Use any()
it return True if any element of the iterable is true. If the iterable is empty, return False.
if not any(line.startswith(x) for x in ["<ul>", "<ol>", "<li>"]):
Upvotes: 0
Reputation: 599778
You can use any
with a list comprehension or generator:
if not any(line.startswith(tag) for tag in ['<ul>', '<ol>', '<li>']):
Upvotes: 2