Checking if List contains all items from another list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether one Python list contains all items from another is simple only after you define what "contains" means. Sometimes duplicates matter, sometimes order matters, and sometimes you only care about set membership. The correct implementation depends on that rule, and different rules lead to different algorithms.
Case 1: Membership Only, Duplicates Do Not Matter
If you only need to know whether every distinct item from one list exists in another, sets are the clearest option.
This is efficient and readable, but it ignores duplicate counts.
Case 2: Duplicates Matter
If [1, 1, 2] should not be considered contained by [1, 2], use collections.Counter.
This compares multiplicity, which is usually the correct rule for inventory-like or occurrence-sensitive problems.
Case 3: Straightforward Readability With all
For small lists, a direct all check is perfectly fine.
This is readable, but it may become slower on large lists because each membership test is linear for plain lists.
Order-Sensitive Variant
If the order of appearance matters, you are solving a different problem. You may need subsequence logic rather than simple containment.
This checks whether required items appear in the same relative order.
Performance Tradeoffs
The right choice depends on input size and semantics:
- '
all(item in list ...)is fine for small inputs.' - '
set-based checks are good for large membership-only comparisons.' - '
Counteris best when duplicate counts matter.'
Choosing set for everything is a mistake if duplicates carry meaning.
Custom Objects Need Equality Rules
If lists contain custom objects, containment depends on how equality is defined. For dataclasses or classes, make sure __eq__ behaves the way your domain expects.
Without well-defined equality, containment checks may produce surprising results.
Normalize Inputs When Needed
Containment checks can also fail because the two lists use different data formats, such as mixed casing or whitespace.
This is a domain rule, not a generic default, but it is common in user-facing string comparisons.
Practical Decision Guide
Choose the approach by business rule:
- Distinct membership uses
set. - Multiplicity-aware membership uses
Counter. - Ordered inclusion uses subsequence logic.
- Very small lists can use
all(...)for clarity.
Make the rule explicit in the function name so future readers do not have to guess.
Common Pitfalls
- Using sets when duplicate counts actually matter.
- Using list membership loops on very large inputs without thinking about cost.
- Forgetting that order-sensitive checks are a different problem.
- Assuming custom objects compare the way business logic expects.
- Naming helper functions too vaguely, such as
contains_all, when semantics are more specific.
Summary
- There is no single best containment check until you define the exact rule.
- Use
set.issubsetwhen only distinct membership matters. - Use
Counterwhen duplicates matter. - Use subsequence logic when order matters.
- Name your helper by semantics so the implementation stays defensible.

