Python: Check if an iterable contains a specific element
Python Clean Code Tip:
Use
in
to check whether an iterable contains a specific element.👇
lucky_numbers = [1, 23, 13, 1234] BEST_NUMBER = 13 # without in best_number_is_lucky_number = False for number in lucky_numbers: if number == BEST_NUMBER: best_number_is_lucky_number = True print(best_number_is_lucky_number) # => True # with in best_number_is_lucky_number = BEST_NUMBER in lucky_numbers print(best_number_is_lucky_number) # => True