Explain Codes LogoExplain Codes Logo

Check if something is (not) in a list in Python

python
performance
best-practices
dataframe
Anton ShumikhinbyAnton Shumikhin·Sep 13, 2024
TLDR

For a presence check: item in my_list. For confirming absence: item not in my_list. Here's how:

my_list = ['a', 'b', 'c'] item, item_not_in_list = 'c', 'z' # Checking presence if item in my_list: print(f"Item '{item}' is part of the party!") # Checking absence if item_not_in_list not in my_list: print(f"Item '{item_not_in_list}' didn't get the invite!")

These expressions are simple yet powerful tools for membership testing in Python.

Leveraging sets for large data

When dealing with monstrous datasets, Python offers a heavier weapon - sets. Convert your list to a set for blazing fast membership testing (performance perks due to their implementation as hash tables):

# Converting the list to a set my_set = set(my_list) if item_not_in_list not in my_set: print(f"Item '{item_not_in_list}': still nope!")

Just remember item types added to sets need to be immutable (and thus, hashable).

Tuple special case

If you're looking to confirm a tuple's absence in a list—a common gotcha!—ensure your magnifying glass is pointed at identical inspector and suspect:

list_of_tuples = [('a', 'b'), ('c', 'd'), ('e', 'f')] tuple_not_in_list = ('x', 'y') # Same structure comparison if tuple_not_in_list not in list_of_tuples: print(f"The tuple {tuple_not_in_list} is not part of this tuple club.")

Comparing disparate types (like integer vs tuple of integer) can give you false negatives... or a (friendly) pythonic slap!

Level up: advanced options

Deploying ‘contains

The not in operator goes undercover and calls the suspect object's __contains__ method if available. Implement this method in your classes to modify how membership checks work:

class MyContainer: def __init__(self, data): self._data = data def __contains__(self, item): return item in self._data # Membership test logic container = MyContainer(my_list) if 3 in container: print("Container booped item. It's a part of team!")

Frequency check with list.count

For those times when you want to play detective on not just the presence but the recurring pattern, meet list.count:

frequency = my_list.count(item_not_in_list) if frequency == 0: print(f"Item '{absent_item}': Nope, still absent.") else: print(f"Item '{absent_item}' is playing hide and repeat {frequency} times!")

But remember, count does a full list scrutiny, so pack some patience for large lists.

Doing "in" on the fast lane

Python's in operator is not a workaholic and short-circuits when it finds the item. If you often check for items hanging near the start of the list, this will save you computation power and time:

# Sorted list with your favorite numbers my_ordered_list = sorted(my_list) # Finds '2' faster if it's near the start if 2 in my_ordered_list: print("2 strikes again! Found it early!")

Datatypes and gotchas

Keep a keen eye on item types when deploying not in. Unexpected villains may appear if the types in your list are not what you assume:

mixed_list = ['2', 2, 'three', 3] if '2' not in mixed_list: # '2' is a string in this lineup, not an integer print("'2' is playing hide and seek.") else: print("'2' got busted. It IS a part of the list.")

Trust no one—always be explicit in your search.