Check if value exists in a Two-dimensional list in Python

avatar

Borislav Hadzhiev

Last updated: Jun 16, 2022

banner

Photo from Unsplash

Check if value exists in a Two-dimensional list in Python #

Use the any() function to check if a value exists in a two-dimensional list, e.g. if any('value' in nested_list for nested_list in my_2d_list):. The any() function will return True if the value exists in the list and False otherwise.

main.py
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('three' in nested_list for nested_list in my_2d_list): # 👇️ this runs print('three is contained in the two-dimensional list') else: print('three is NOT contained in the two-dimensional list') # ------------------------------------------------------------------ # ✅ if you need to get the sublist that contains the value if any('three' in (match := nested_list) for nested_list in my_2d_list): # 👇️ this runs print('three is contained in the two-dimensional list') print(match) # 👉️ ['three', 'four'] print(match.index('three')) # 👉️ 0 else: print('three is NOT contained in the two-dimensional list')

We used a generator expression to iterate over the two-dimensional list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if a specific value is contained in the nested list.

main.py
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('three' in nested_list for nested_list in my_2d_list): # 👇️ this runs print('three is contained in the two-dimensional list') else: print('three is NOT contained in the two-dimensional list')

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

If the specified value is found in any of the nested lists, the any() function short-circuits returning True.

You can use the assignment expression syntax if you need to get the nested list that contains the specified value.

main.py
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('three' in (match := nested_list) for nested_list in my_2d_list): # 👇️ this runs print('three is contained in the two-dimensional list') print(match) # 👉️ ['three', 'four'] print(match.index('three')) # 👉️ 0 else: print('three is NOT contained in the two-dimensional list')

Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

If the iterable is empty or none of the elements in the iterable are truthy, the any function returns False.

main.py
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any('HELLO' in nested_list for nested_list in my_2d_list): print('HELLO is contained in the two-dimensional list') else: # 👇️ this runs print('HELLO is NOT contained in the two-dimensional list')

None of the nested lists contain an item with the specified value, so the condition is never met and the any() function returns False.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.