I have this piece of code which checks conditions:
def is_important(data_row):
if data_row.get('important', None):
return True
add_data = get_additional_data(data_row)
for word in NEGATIVE_WORDS:
if word in add_data.lower():
return False
for word in POSITIVE_WORDS:
if word in add_data.lower():
return True
return False
This is quite hard to follow (in my opinion), so I was wondering if anyone can suggest something more pythonic with shorter lines? Could I for example merge the two for loops? If I merge two for loops, would it consumes more time?
That is more compact and short-circuits due to the
any
s much like your explicit loops did.A couple of things on that:
.lower()
when searching forNEGATIVE_WORDS
and not forPOSITIVE_WORDS
?add_data
contain bothNEGATIVE_WORDS
andPOSITIVE_WORDS
, the order of the last twoif
s will affect the outcome. This is not good practice.