I want to simplify the body of my current code which involves comparative operators. The operators were given to me in a string format, so I thought of using if/elif
statements to return the result of the comparison.
Below is the body of my code:
if condition[1] == '<':
return True if item[condition[0]] < condition[2] else False
elif condition[1] == '<=':
return True if item[condition[0]] <= condition[2] else False
elif condition[1] == '==':
return True if item[condition[0]] == condition[2] else False
...and the same format is also used for other available comparative operators such as !=
and >=
.
I noticed that there is a repetition and this should be easy to solve, but I can't seem to think of a way to simplify this into a shorter code.