Membership Testing
4 min readFeb 9
--
Membership testing means checking if a collection of items (a list, a set, a dictionary, etc.) contains a specific item. For example, checking if a list of even numbers contains number 42. It’s a quite common operation, so let’s see how to do it properly.
How can we check if a list contains a specific item? There is a terrible way of doing this — iterating through the list in a “for loop”: