r/learningpython • • Jun 26 '20

Which one is cleaner to use?

if variable.attr in (1, 2):
    some_list.append(variable)

or

if variable.attr == 1:
    some_list.append(variable)
elif variable.attr == 2:
    some_list.append(variable)

The code executed inside of the if statement will always be a single line of code

2 Upvotes

4 comments sorted by