Python's in set operator
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview
Python offers a diverse set of operators to manipulate collections, and one of the most commonly used operators is the `in` keyword. The `in` operator checks for membership and is highly efficient when applied to sets. This article provides a comprehensive look at how the `in` operator functions with sets, including detailed explanations, examples, performance considerations, and more.
Understanding Sets in Python
A set in Python is a collection of distinct, immutable objects. Unlike lists or tuples, a set does not allow duplicate elements, which makes operations like checking membership quick and efficient. Sets in Python are similar to mathematical sets and are implemented as hash tables, providing average-case time complexity of for membership tests, adding elements, and removing elements.
The `in` Operator
The `in` operator is used to verify if a particular item exists within a set. It returns `True` if the item is present and `False` otherwise. Given that sets are unordered, the `in` operator uses the hashing of the element to make this determination, allowing it to quickly verify the presence of an object without needing to iterate over every element.
Syntax
- item: The element you are looking for.
- set: The set you are searching in.
- Efficient Membership Testing: As mentioned, sets offer average-time complexity for membership tests, which is significantly faster than the average-case time complexity of lists, which is .
- Automatic Duplicate Elimination: Sets automatically eliminate duplicates, making them a perfect choice when you need to filter unique items.
- Additional Set Operations: Sets provide several useful operations such as union, intersection, and difference, which can be combined with membership testing.
- Lists: If you require ordered collections, which allow duplicates, then a list may be more appropriate. However, membership tests will take time on average.
- Dictionaries: If you need key-value pairs, Python dictionaries are another efficient option. The `in` operator checks for key existence and operates with similar performance as sets for keys.
Related reading
- Python's most efficient way to choose longest string in list?
- Python's underlying hash data structure for dictionaries
- QuadTree find neighbor
- Query for documents where array size is greater than 1
- Python's time.clock vs. time.time accuracy?
- pythonw.exe or python.exe?
- Query regarding dijkstra algorithm
- Question from Interview, Retrieve alphabetic order from dictionary

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.