Check if one list contains element from the other
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding List Containment in Python
List operations are a fundamental aspect of programming. One common task is to verify if one list contains elements from another list. In Python, this can be achieved using various techniques ranging from simple loops to powerful built-in functions and libraries. This article dives deep into these methods, offering detailed explanations, examples, and a comparison table for clarity.
Introduction to Lists in Python
Lists are ordered collections in Python that allow for the storage of multiple items. They are versatile and useful for a variety of applications due to their mutable nature.
Methods to Check Containment
1. Using a For Loop
A straightforward method involves iterating over one list and checking if any of its elements exist in the other list.
Explanation:
- This approach checks each element of
list_ato see if it is present inlist_b. - The
inkeyword is used for membership checking.
2. Using Set Intersection
A more efficient method utilizes set operations, which can leverage hashing for faster lookups.
Explanation:
- Convert
list_ato a set and check if it is disjoint fromlist_b. isdisjoint()is a method that returnsTrueif two sets have no elements in common.
3. Using List Comprehension
List comprehension can also be employed for a more Pythonic solution.
Explanation:
- This makes use of the
any()function, which returnsTrueif any element in the list comprehension isTrue.
4. Using the Built-in any() Function
Combining any() with generator expressions is another elegant method to solve the problem.
Explanation:
- Similar to comprehension, but uses a generator expression resulting in better performance due to lazy evaluation.
Performance Considerations
The choice between these methods could affect performance, especially with larger lists. Here is a comparison:
| Method | Time Complexity | Pros | Cons |
| For Loop | Simple and intuitive | Potentially less efficient for large lists | |
| Set Intersection | Fast for large lists | Conversion overhead | |
| List Comprehension | Pythonic and concise | Similar performance to loops | |
any() with Generator Expression | Short-circuits on first match | Potential overuse of memory on large datasets |
Additional Considerations
- Immutable Collections: If your data does not change, consider using tuples, which can offer performance gains for specific use cases.
- Order Maintenance: If maintaining the order of elements is crucial, some set operations may not be suitable.
- Duplicates: List contains checks do not typically handle duplicate elements unless explicit checks are added.
Conclusion
The need to check if one list contains elements from another is a common requirement in programming. The method to choose depends on the specific needs of your application, such as the size of the lists and performance considerations. Understanding these various techniques allows for optimal and effective programming practices.
Related reading
- Check if polygon is inside a polygon
- Check if sum possible in array
- Check if the given string follows the given pattern
- Check if two arrays are cyclic permutations
- Check if optional array is empty
- Check if optional array is empty
- Check if something is not in a list in Python
- Check if string ends with one of the strings from a list

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.