Check if a List is Contained in Another List - Python
Last Updated :
01 Jul, 2025
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 list is present in the larger list (without breaking order), then we say the first list is contained in the second.
Input : A = [1, 2], B = [1, 2, 3, 1, 1, 2, 2]
Output : True
Input : A = ['x', 'y', 'z'], B = ['x', 'a', 'y', 'x', 'b', 'z']
Output : False
Let's discuss different methods to solve this.
Using list comprehension
List comprehension is a concise way to check if all elements of one list appear in the same order within another list. It works by comparing slices of one list to another during iteration.
Python
A = [1, 2]
B = [1, 2, 3, 1, 1, 2, 2]
n = len(A)
r = any(A == B[i:i + n] for i in range(len(B) - n + 1))
print(r)
Explanation:
- B[i:i+n] takes slices of list B of the same length as A.
- A == B[i:i+n] checks if any slice matches A exactly and in order.
- any() returns True if at least one match is found.
Using Nested loop
Using two for loops, one to go through the larger list and another to compare elements of the smaller list at each position. If all elements match in order, it returns True otherwise, False.
Python
A = [1, 2]
B = [1, 2, 3, 1, 1, 2, 2]
f = False
for i in range(len(B) - len(A) + 1):
for j in range(len(A)):
if B[i + j] != A[j]:
break
else:
f = True
break
print(f)
Explanation:
- Outer loop checks all possible starting positions in B where A could fit.
- Inner loop compares elements in A with corresponding elements in B starting from position i.
- sets f = True and breaks, meaning match found.
Using Numpy
NumPy compare sublists by turning both lists into arrays and checking if any slice of the larger array matches the smaller one.
Python
import numpy as np
A = ['x', 'y', 'z']
B = ['x', 'a', 'y', 'x', 'b', 'z']
a1 = np.array(A)
b1 = np.array(B)
f = False
for i in range(len(b1)):
if np.array_equal(a1, b1[i:i+len(a1)]):
f = True
break
print(f)
Explanation: checks if list A appears as a continuous sublist in list B by converting both to NumPy arrays and comparing slices using np.array_equal. Not found in same order, so False.
Related Articles:
Similar Reads
Python Check if the List Contains Elements of another List 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
3 min read
Python | Check if a list exists in given list of lists Given a list of lists, the task is to check if a list exists in given list of lists. Input : lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]] list_search = [4, 5, 6] Output: True Input : lst = [[5, 6, 7], [12, 54, 9], [1, 2, 3]] list_search = [4, 12, 54] Output: False Letâs discuss certain ways
4 min read
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
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
Check if a Nested List is a Subset of Another Nested List - Python The task is to check if all sublists in one nested list are present in another nested list. This is done by verifying whether each sublist in the second list exists in the first list.For example, given list1 = [[2, 3, 1], [4, 5], [6, 8]] and list2 = [[4, 5], [6, 8]], we check if both [4, 5] and [6,
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