Last updated: Apr 8, 2024
Reading timeยท3 min
Use the any()
function to check if a value exists in a two-dimensional
list.
The any()
function will return True
if the value exists in the list and
False
otherwise.
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')
We used a generator expression to iterate over the two-dimensional list.
On each iteration, we check if a specific value is contained in the nested list.
The any function
takes an iterable as an argument and returns True
if any element in the
iterable is truthy.
any()
function short-circuits returning True
.If you have to do this often, define a reusable function.
def is_in_2d_list(two_dimensional_list, value): return any( value in nested_list for nested_list in two_dimensional_list ) my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] print(is_in_2d_list(my_2d_list, 'one')) # ๐๏ธ True print(is_in_2d_list(my_2d_list, 'abc')) # ๐๏ธ False if is_in_2d_list(my_2d_list, 'one'): print('The value is in the two-dimensional list') else: print('The value is NOT in the two-dimensional list')
The function takes a two-dimensional list and a value as parameters and returns
True
if the value is contained in the two-dimensional list and false
otherwise.
You can use the assignment expression syntax if you need to get the nested list that contains the specified value.
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
.
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
.
Alternatively, you can use a for loop.
for
loopThis is a three-step process:
for
loop to iterate over the list of lists.in
operator to check if each sublist contains the value.my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] exists_in_list = False for nested_list in my_2d_list: if 'three' in nested_list: exists_in_list = True print(nested_list) break if exists_in_list: print('The value exists in the two-dimensional list') else: print('The value does NOT exist in the two-dimensional list')
We used a for
loop to iterate over the two-dimensional list.
On each iteration, we check if the current sublist contains a given value.
If the value is contained in the sublist, we set the exists_in_list
variable
to True
and break out of the loop.
The break statement breaks out of the
innermost enclosing for
or while
loop.
If the value is not contained in the two-dimensional list, the exists_in_list
variable remains set to False
.
You can learn more about the related topics by checking out the following tutorials: