The in operator allows you to check if an element exists in a set. It returns True if the element is found, False otherwise.
↗️
Loading editor...
↶
↷
The in operator is very fast with sets because sets use hash tables internally. This makes membership testing much more efficient than with lists.
Sets vs Lists for Membership Testing
When you need to frequently check if elements exist in a collection, sets are much faster than lists. With lists, Python has to check each element one by one. With sets, it can find elements almost instantly.