# Inefficient membership testing When checking if any element in one [[Iterable]] exists in another, using a [[GenExp - Generator Expression|generator expression]] or a [[ListComp - List Comprehension|list comprehension]] with `any()` can be inefficient, especially for large datasets. This [[anti-pattern]] can lead to an unexpected high computational cost. ```python # Don't if any(b in foo for b in bar): ... # Do if set(bar) & set(foo): ... ``` Using [[Set]] operations (`&` for intersection) improves performance significantly. Converting both sequences into sets allows for an efficient membership test in **O(1) average [[Algorithmic Complexity|time complexity]]** rather than **O(n) per lookup** in a [[List]], making the entire operation **O(n)** instead of **O(n²)**. --- Bibliography: - [Title - website.com](need bibliography)