Check if multiple Values are in a List in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
5 min

banner

# Table of Contents

  1. Check if multiple Values are in a List in Python
  2. Check if One of multiple Values is in a List in Python

# Check if multiple values are in a list in Python

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.

main.py
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))

check if multiple values are in list

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.

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 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.

main.py
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.

# Check if multiple values are in a List using a Set object

Alternatively, you can use the set() class to convert the collection of values to a set object to be able to use the issubset() method.

main.py
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))

check if multiple values in list using set

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.

# Check if multiple values are in a List using a for loop

You can also use a for loop to check if multiple values are in a list.

main.py
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')

check if multiple values are in list using for loop

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.

# Check if One of Multiple Values is in a List in Python

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.

main.py
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')

check if one of multiple values is in list

We used a generator expression to iterate over a list.

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

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.

# Check if One of Multiple Values is in a List and get the value

You can use the assignment expression syntax if you need to get the value that is contained in the list.

main.py
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')

check if one of multiple values is in list and get the value

Assignment expressions allow us to assign to variables within an expression using the 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.

main.py
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.

# Check if One of Multiple Values is in a List using a for loop

You can also use a for loop to check if one of multiple values is in a list.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev