Open In App

Python Check if the List Contains Elements of another List

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

The task of checking if a list contains elements of another list in Python involves verifying whether all elements from one list are present in another list. For example, checking if ["a", "b"] exists within ["a", "b", "c", "d"] would return True, while checking ["x", "y"] would return False.

Using issubset()

issubset() is a highly efficient approach as it transforms both lists into sets, allowing quick membership checks. It is the preferred choice when order and duplicates don’t matter.

Python
a = ["a", "b", "c", "d"]  # main list
b = ["a", "b"]            # list to check

if set(b).issubset(a):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation: It first converts b into a set and uses the issubset() method to check if every element in b exists in a. If this condition is true, it prints "Yes" otherwise, it prints "No".

Using all()

This method checks each element of one list against the other using a concise and readable generator expression inside the all() function. It stops as soon as a missing element is found, making it more efficient than a plain loop.

Python
a = ["a", "b", "c", "d"]  # main list
b = ["a", "b"]            # list to check

if all(e in a for e in b):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation: It iterates through each element e in b and checks if it exists in a. If every element in b is found in a, it prints "Yes" otherwise prints "No".

Using Counter()

Counter() class from the collections module creates a frequency dictionary that maps each element to its count in a list. This is particularly useful when it is important to verify the number of times each element appears in the list. If we need to check whether one list contains all the elements of another list with at least the same frequency, Counter() is the ideal approach.

Python
from collections import Counter

a = ["a", "b", "c", "d"]  # main list
b = ["a", "b"]            # list to check

a_counter = Counter(a)
b_counter = Counter(b)

if all(b_counter[element] <= a_counter[element] for element in b_counter):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation: Counter() from collections, this approach checks if list a contains all elements of list b with at least the same frequency by comparing element counts. It prints "Yes" if the condition holds; otherwise, "No".

Using set intersection

Set Intersection approach converts both lists into sets to check if all unique elements of one list exist in the other, ignoring order and duplicates. It compares the intersection with the smaller set, making it suitable for simple presence checks.

Python
a = ["a", "b", "c", "d"]  # main list
b = ["a", "b"]            # list to check

if set(b).intersection(a) == set(b):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation: It converts both lists into sets and compares the intersection with set b. If the intersection is equal to b's set, it prints "Yes" otherwise, "No".


Next Article
Practice Tags :

Similar Reads