In this short tutorial, you will learn to check if a Python list contains all the elements of another list and show the result using the print() function.
Program to Check List Contains Elements of Another List
We can solve this using different methods. Each technique is explained using a demo program. To understand them, make sure you have basic Python programming knowledge.
Problem: Check Python List Contains Elements of Another List
Write a demo program to check if a list contains elements of another list. You have two lists having overlapping values. One of these is the big one which holds all the elements of the second one.
Solutions
Now, we’ve to programmatically prove that List1 contains the elements of List2. As stated earlier, there are multiple ways to achieve it. Let’s dive in to learn them.
all() method
To demonstrate that List1 has List2 elements, we’ll use the all() method.
# Program to check the list contains elements of another list
# List1
List1 = ['python' , 'javascript', 'csharp', 'go', 'c', 'c++']
# List2
List2 = ['csharp1' , 'go', 'python']
check = all(item in List1 for item in List2)
if check is True:
print("The list {} contains all elements of the list {}".format(List1, List2))
else :
print("No, List1 doesn't have all elements of the List2.")
The output of the above code is as follows:
The list ['python', 'javascript', 'csharp', 'go', 'c', 'c++'] contains all elements of the list ['csharp', 'go', 'python']
Also Read: Python Get the Last Element in a List
any() method
Another method is any() which we can use to check if the list contains any elements of another one.
# Program to check the list contains elements of another list # List1 List1 = ['python' , 'javascript', 'csharp', 'go', 'c', 'c++'] # List2 List2 = ['swift' , 'php', 'python'] check = any(item in List1 for item in List2) if check is True: print("The list {} contains some elements of the list {}".format(List1, List2)) else : print("No, List1 doesn't have any elements of the List2.")
The output of the above code is as follows:
The list ['python', 'javascript', 'csharp', 'go', 'c', 'c++'] contains some elements of the list ['swift', 'php', 'python']
in keyword
The “in” keyword is a very efficient way to check if a Python list contains a particular element. However, it can become inefficient for larger lists.
In this method, we’ll write a custom search method to test if the first list contains the second one. While iterating the lists if we get an overlapping element, then the function returns true. The search continues until there is no element to match and returns false.
# Program to check if a Python list contains elements of another list def list_contains(List1, List2): check = False # Iterate in the 1st list for m in List1: # Iterate in the 2nd list for n in List2: # if there is a match if m == n: check = True return check return check # Test Case 1 List1 = ['a', 'e', 'i', 'o', 'u'] List2 = ['x', 'y', 'z', 'l', 'm'] print("Test Case#1 ", list_contains(List1, List2)) # Test Case 2 List1 = ['a', 'e', 'i', 'o', 'u'] List2 = ['a', 'b', 'c', 'd', 'e'] print("Test Case#2 ", list_contains(List1, List2))
The output of the above code is as follows:
Test Case#1 False Test Case#2 True
set() method
We’ll use the set() method to convert the lists and call the Python set intersection() method to find if there is any match between the list elements.
# Program to check if a Python list contains elements of another list def list_contains(List1, List2): set1 = set(List1) set2 = set(List2) if set1.intersection(set2): return True else: return False # Test Case 1 List1 = ['a', 'e', 'i', 'o', 'u'] List2 = ['x', 'y', 'z', 'l', 'm'] print("Test Case#1 ", list_contains(List1, List2)) # Test Case 2 List1 = ['a', 'e', 'i', 'o', 'u'] List2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] print("Test Case#2 ", list_contains(List1, List2))
The output of the above code is as follows:
Test Case#1 False Test Case#2 True
Use collections class
The collections.Counter()
class creates a counter object from a list. A counter object is a dictionary that stores the count of each element in a list.
We can use the collections.Counter() class to check if a Python list contains all elements of another list by comparing the two counter objects. If the two counter objects are equal, then the first list contains all elements of the second list, and in the same quantity.
Here is an example of how to use the collections.Counter() class to check if a Python list contains all elements of another list:
from collections import Counter def list_contains_elements_of_list(list1, list2): """Returns True if list1 contains all elements of list2, False otherwise.""" counter1 = Counter(list1) counter2 = Counter(list2) # Check if counter2 is a subset of counter1 return all(counter2[element] <= counter1[element] for element in counter2) list1 = [1, 2, 3, 4, 5] list2 = [2, 4, 5] list3 = [2, 3, 7] print(list_contains_elements_of_list(list1, list2)) print(list_contains_elements_of_list(list1, list3)) # True # False
We introduced a third list in the above code to showcase a failure case. It means the case when the list1 doesn’t contain all the elements of another list.
Must Read: Python Remove Last Element from a List
Use list comprehension
A list comprehension in Python is a way to create a new list from an existing list. List comprehensions are concise and efficient, and they can be used to perform a variety of tasks, including checking if a list contains all elements of another list.
Here is an example of how to use list comprehension to check if a Python list contains all elements of another list:
def list_contains_elements_of_list(list1, list2): """Returns True if list1 contains all elements of list2, False otherwise.""" return all(element in list1 for element in list2) list1 = [1, 2, 3, 4, 5] list2 = [2, 4, 5] list3 = [2, 3, 7] print(list_contains_elements_of_list(list1, list2)) print(list_contains_elements_of_list(list1, list3)) # True # False
In this tutorial, you have learned how to check if a Python list contains all elements of another list. We have explored six different methods for doing this including any() and all() functions.
Feel free to use these in your Python programs to meet your specific use case. We’ll be happy to hear about your experience once you use them.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.