Python - Check if a list is contained in another list

Given two different Python lists, we need to find if the first list is a part of the second list as a contiguous subsequence. Python provides several approaches to check if one list is contained within another.

Using map() and join()

We can convert both lists to comma-separated strings using map() and join(), then use the in operator to check containment ?

fruits = ['apple', 'banana', 'cherry']
inventory = ['cherry', 'grape', 'apple', 'banana', 'cherry', 'kiwi']

print("Given fruits elements:")
print(', '.join(map(str, fruits)))
print("Given inventory elements:")
print(', '.join(map(str, inventory)))

# Convert to strings and check containment
fruits_str = ', '.join(map(str, fruits))
inventory_str = ', '.join(map(str, inventory))
result = fruits_str in inventory_str

if result:
    print("fruits list is part of inventory list")
else:
    print("fruits list is not part of inventory list")
Given fruits elements:
apple, banana, cherry
Given inventory elements:
cherry, grape, apple, banana, cherry, kiwi
fruits list is part of inventory list

Using range() and len()

We can use a sliding window approach with range() and len() to check each possible subsequence ?

fruits = ['apple', 'banana', 'cherry']
inventory = ['cherry', 'grape', 'apple', 'banana', 'cherry', 'kiwi']

print("Given fruits elements:")
print(fruits)
print("Given inventory elements:")
print(inventory)

n = len(fruits)
result = any(fruits == inventory[i:i + n] for i in range(len(inventory) - n + 1))

if result:
    print("fruits list is part of inventory list")
else:
    print("fruits list is not part of inventory list")
Given fruits elements:
['apple', 'banana', 'cherry']
Given inventory elements:
['cherry', 'grape', 'apple', 'banana', 'cherry', 'kiwi']
fruits list is part of inventory list

Using all() with Element-wise Check

For checking if all elements exist (not necessarily contiguous), we can use all() with a generator expression ?

fruits = ['apple', 'banana']
inventory = ['cherry', 'apple', 'grape', 'banana', 'kiwi']

print("Checking if all fruits exist in inventory:")
print("fruits:", fruits)
print("inventory:", inventory)

# Check if all elements of fruits exist in inventory
result = all(item in inventory for item in fruits)

if result:
    print("All fruits are available in inventory")
else:
    print("Some fruits are not available in inventory")
Checking if all fruits exist in inventory:
fruits: ['apple', 'banana']
inventory: ['cherry', 'apple', 'grape', 'banana', 'kiwi']
All fruits are available in inventory

Comparison

Method Checks For Performance Best For
map() + join() Contiguous sequence O(n+m) String-based matching
range() + len() Contiguous sequence O(n*m) Exact order matching
all() + in Element existence O(n*m) Any order matching

Conclusion

Use the sliding window approach with range() for exact contiguous subsequence matching. Use all() with in when order doesn't matter and you only need to check element existence.

Updated on: 2026-03-15T18:15:02+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements