Check if a list contains elements of another list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 10, 2022

banner

Photo from Unsplash

Check if a list contains elements of another list in Python #

To check if a list contains any elements of another list:

  1. Use a generator expression to iterate over the list.
  2. Check if any elements in the first list are contained in the second list.
  3. The any() method will return True if the list contains any elements of the other list.
main.py
list_1 = ['apple', 'banana', 'kiwi'] list_2 = ['melon', 'avocado', 'apple'] # ✅ check if a list contains ANY elements of another list if any(item in list_2 for item in list_1): # 👇️ this runs print('list_2 contains at least 1 item present in list_1') else: print('list_2 does NOT contain any of the items in list_1') list_3 = ['apple', 'banana', 'kiwi'] list_4 = ['apple', 'banana', 'kiwi'] # -------------------------------------------------------- # ✅ check if a list contains ALL elements of another list if all(item in list_4 for item in list_3): # 👇️ this runs print('all items in list_3 are present in list_4') else: print('NOT all items in list_3 are present in list_4')

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 check if each element in the first list is contained in the second 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 item in the first list is contained in the second.

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

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

main.py
list_1 = ['apple', 'banana', 'kiwi'] list_2 = ['melon', 'avocado', 'apple'] # ✅ check if a list contains ANY elements of another list if any((match := item) in list_2 for item in list_1): # 👇️ this runs print('list_2 contains at least 1 item present in list_1') print(match) # 👉️ 'apple' else: print('list_2 does NOT contain any of the items in list_1')
Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

To check if a list contains all elements of another list:

  1. Use a generator expression to iterate over the list.
  2. Check if each element in the first list is contained in the second list.
  3. The all() method will return True if the list contains all elements of the other list.
main.py
list_1 = ['apple', 'banana', 'kiwi'] list_2 = ['apple', 'banana', 'kiwi'] if all(item in list_2 for item in list_1): # 👇️ this runs print('all items in list_1 are present in list_2') else: print('NOT all items in list_1 are present in list_2')

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

In the example, we check if each item in list_1 is present in list_2.

If the condition is met for all list elements, the all() method returns True, otherwise False is returned.

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.