Python Check if the List Contains Elements of another List
Last Updated :
22 Feb, 2025
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")
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")
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")
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")
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".
Similar Reads
Python - Check if List contains elements in Range Checking if a list contains elements within a specific range is a common problem. In this article, we will various approaches to test if elements of a list fall within a given range in Python. Let's start with a simple method to Test whether a list contains elements in a range.Using any() Function -
3 min read
Check if a List is Contained in Another List - Python In Python, it is often useful to check whether all elements of one list exist in another list. This is known as checking if a list is contained in another list. This is commonly used in tasks like filtering, searching and validating data.For example, given two lists, if every item in the smaller lis
3 min read
Check if any element in list satisfies a condition-Python The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio
2 min read
Python - Check if two lists have at-least one element common Checking if two lists share at least one common element is a frequent task when working with datasets, filtering, or validating data. Python offers multiple efficient ways to solve this depending on the size and structure of the lists.Using set IntersectionConverting both lists into sets and finding
3 min read
Python - Check List elements from Dictionary List Sometimes, while working with data, we can have a problem in which we need to check for list element presence as a particular key in list of records. This kind of problem can occur in domains in which data are involved like web development and Machine Learning. Lets discuss certain ways in which thi
4 min read
Python | Check if two lists have any element in common Checking if two lists share any common elements is a frequent requirement in Python. It can be efficiently handled using different methods depending on the use case. In this article, we explore some simple and effective ways to perform this check.Using set IntersectionSet intersection uses Python's
3 min read