Open In App

Test if all elements are present in list-Python

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Output
True

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

Output
True

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

Output
True

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

Output
True

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.


Next Article
Practice Tags :

Similar Reads