Simplify Chained Comparison – Dev

The best answers to the question “Simplify Chained Comparison” in the category Dev.


I have an integer value x, and I need to check if it is between a start and end values, so I write the following statements:

if x >= start and x <= end:
    # do stuff

This statement gets underlined, and the tooltip tells me that I must

simplify chained comparison

As far as I can tell, that comparison is about as simple as they come. What have I missed here?


It can be rewritten as:

start <= x <= end:


r = range(start, end + 1) # (!) if integers
if x in r:


In Python you can “chain” comparison operations which just means they are “and”ed together. In your case, it’d be like this:

if start <= x <= end: