Membership Testing

Sebastian Witowski
4 min readFeb 9, 2023

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”:

--

--

Sebastian Witowski

Python consultant and freelancer at switowski.com. Writes about productivity, tools, Python, and programming best practices.