Test if all elements are present in list-Python
Last Updated :
05 Feb, 2025
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.
Using set intersection
Set intersection method is one of the most efficient way to test if all elements of one list are present in another. By converting both lists to sets, we can use fast set operations like issubset() to determine if all elements in list 2 are also in list 1. This approach benefits from the optimized time complexity of set operations, making it ideal for larger datasets.
Python
a = [6, 4, 8, 9, 10]
b = [4, 6, 9]
res = set(b).issubset(set(a))
print(str(res))
Explanation: set(b).issubset(set(a)) checks if all elements of b are in a by converting both lists to sets and using the issubset().
Using hash set
In this method, we convert the first list into a set, which allows for constant time membership checking. By iterating through the second list and checking each element's presence in the hash set, we efficiently test if all elements in the second list exist in the first. This method avoids the need to check membership in a list, significantly improving performance.
Python
a = [6, 4, 8, 9, 10]
b = [4, 6, 9]
set_a = set(a)
res = all(ele in set_a for ele in b)
print(str(res))
Explanation: set_a = set(a) converts list a into a set for efficient membership checks and res = all(ele in set_a for ele in b) checks if all elements of b are in set_a and returns True if they are.
Using list comprehension
List comprehension with a generator expression offers a more concise way to check if all elements of one list are present in another. The all() function combined with list comprehension evaluates the condition for each element in the second list, returning True only if all elements are found in the first list.
Python
a = [6, 4, 8, 9, 10]
b = [4, 6, 9]
res = all([ele in a for ele in b])
print(str(res))
Explanation: List comprehension iterates over each element in list b and checks if it is present in list a and all() function then returns True only if all elements of b are found in a.
Using loop
A straightforward method to check if all elements of one list are in another is by using a loop and the in operator. This approach iterates through each element of the second list and checks if it exists in the first list. While simple, this method can become inefficient for large lists due to the repeated scans of the first list.
Python
a = [6, 4, 8, 9, 10]
b = [4, 6, 9]
res = True # Assume all elements in b are in a
for ele in b:
if ele not in a:
res = False
break
print(str(res))
Explanation: For loop iterates over b, checking if each element is in a. If an element is missing, res is set to False and the loop breaks early.
Similar Reads
Python - Check if any list element is present in Tuple Given a tuple, check if any list element is present in it. Input : test_tup = (4, 5, 10, 9, 3), check_list = [6, 7, 10, 11] Output : True Explanation : 10 occurs in both tuple and list. Input : test_tup = (4, 5, 12, 9, 3), check_list = [6, 7, 10, 11] Output : False Explanation : No common elements.
6 min read
Python - Test if all elements in list are of same type When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Python - Check if element is present in tuple We are given a tuple and our task is to find whether given element is present in tuple or not. For example x = (1, 2, 3, 4, 5) and we need to find if 3 is present in tuple so that in this case resultant output should be True.Using in Operatorin operator checks if an element is present in a tuple by
2 min read
Python - Check if all elements in List are same To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. Pythona = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) OutputTrue Explanation:Converting
3 min read
Python | Check if element is present in tuple of tuples Sometimes the data that we use is in the form of tuples and often we need to look into the nested tuples as well. The common problem that this can solve is looking for missing data or na value in data preprocessing. Let's discuss certain ways in which this can be performed. Method #1: Using any() an
4 min read