Last updated: Apr 9, 2024
Reading timeยท5 min
Use the all()
function to check if multiple values are in a list.
The all()
function will return True
if all of the specified values are in
the list and False
otherwise.
my_list = ['one', 'two', 'three', 'four', 'five'] multiple_values = ['one', 'two', 'three'] if all(value in my_list for value in multiple_values): # ๐๏ธ this runs print('All of the values are in the list') else: print('Not all of the values are in the list') # ๐๏ธ True print(all(value in my_list for value in multiple_values))
If you need to check if one of multiple values is in a list, click on the following subheading:
We used a generator expression to iterate over the collection of multiple values.
On each iteration, we check if the current value is present in the list and return the result.
The all() built-in
function takes an iterable as an argument and returns True
if all elements in
the iterable are truthy (or the iterable is empty).
If one of the values isn't present in the list, the all()
function will
short-circuit returning False
.
my_list = ['one', 'two', 'three', 'four', 'five'] multiple_values = ['fifty', 'two', 'three'] if all(value in my_list for value in multiple_values): print('All of the values are in the list') else: # ๐๏ธ this runs print('Not all of the values are in the list') # ๐๏ธ False print(all(value in my_list for value in multiple_values))
The string fifty
is not contained in the list, so the all()
function
short-circuited returning False
.
Set
objectAlternatively, you can use the set()
class to convert the collection of values
to a set
object to be able to use the issubset()
method.
my_list = ['one', 'two', 'three', 'four', 'five'] multiple_values = ['one', 'two', 'three'] if set(multiple_values).issubset(my_list): # ๐๏ธ this runs print('All of the values are in the list') else: print('Not all of the values are in the list') # ๐๏ธ True print(set(multiple_values).issubset(my_list))
The
set.issubset()
method tests if every element of the set
is in the provided sequence.
The condition evaluates to True
only if all of the specified values are
present in the list.
for
loopYou can also use a for loop to check if multiple values are in a list.
my_list = ['one', 'two', 'three', 'four', 'five'] multiple_values = ['one', 'two', 'three'] multiple_in_list = True for value in multiple_values: if value not in my_list: multiple_in_list = False break print(multiple_in_list) # ๐๏ธ True if multiple_in_list: print('The multiple values are in th elist') else: print('Not all of the values are in the list')
We initialized the multiple_in_list
variable to True
and used a for
loop
to iterate over the list of values.
On each iteration, we check if the current value is not contained in the other list.
If the condition is met, we set the multiple_in_list
variable to False
and
exit the for
loop.
Use the any()
function to check if one of multiple values is in a list.
The any()
function will return True
if at least one of the values is in
the list and False
otherwise.
my_list = ['one', 'two', 'three'] multiple_values = ['four', 'five', 'three'] # โ Check if one of multiple values is in a list if any(item in my_list for item in multiple_values): # ๐๏ธ this runs print('At least one of the values is in the list') else: print('None of the values are in the list')
We used a generator expression to iterate over a list.
In the example, we iterate over the multiple values collection and check if each value is contained in the list.
The any function
takes an iterable as an argument and returns True
if any element in the
iterable is truthy.
The any()
function will short-circuit returning True
if at least one value
is contained in the list.
You can use the assignment expression syntax if you need to get the value that is contained in the list.
my_list = ['one', 'two', 'three'] multiple_values = ['four', 'five', 'three'] if any((match := item) in my_list for item in multiple_values): # ๐๏ธ this runs print('At least one of the values is in the list') print(match) # ๐๏ธ 'three' else: print('None of the values are in the list')
NAME := expression
syntax.If the condition is met at least once, the any()
function returns True
.
If the iterable we pass to the any()
function is empty or none of the elements
in the iterable are truthy, the any
function returns False
.
my_list = ['one', 'two', 'three'] multiple_values = ['four', 'five', 'six'] if any((match := item) in my_list for item in multiple_values): print('At least one of the values is in the list') print(match) else: # ๐๏ธ this runs print('None of the values are in the list')
None of the multiple values is contained in the list, so the condition is never
met and any()
returns False
.
for
loopYou can also use a for
loop to check if one of multiple values is in a list.
my_list = ['one', 'two', 'three'] multiple_values = ['four', 'five', 'three'] one_in_list = False for value in multiple_values: if value in my_list: one_in_list = True break print(one_in_list) # ๐๏ธ True if one_in_list: # ๐๏ธ this runs print('At least one of the values is in the list.') else: print('None of the values are in the list.')
We initialized the one_in_list
variable to False
and used a for
loop to
iterate over the list of values.
On each iteration, we check if the current value is contained in the original list.
If the condition is met, we set the one_in_list
variable to True
and exit
the loop.
The break statement breaks out of the
innermost enclosing for
or while
loop.
You can learn more about the related topics by checking out the following tutorials: