Itβs easier to extend the first approach to more lines:
if var in ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']:
The second approach is quickly becoming cumbersome:
if var == 'one' or var == 'two' or var == 'three' or ...:
In addition, the first approach can be used to check the return value of a function call and evaluates it only once:
if bigCalculation() in ['one', 'two', 'three']:
The second approach is to reevaluate the call every time, which leads to worse performance (especially if the call is expensive).
if bigCalculation() == 'one' or bigCalculation() == 'two' or ...:
source share