Python List Intersection: Sets, Duplicates, Order, and Multiple Lists

Quick answer: Choose a list-intersection method from the output contract. Set intersection is fast and removes duplicates, a set-backed comprehension preserves the first list’s order, and Counter computes duplicate-aware multiset intersection.

Python Pool infographic comparing Python list intersection sets Counter order preservation duplicates and multiple lists
Choose a list-intersection method from the required contract: unique membership, duplicate counts, stable order, or multiple inputs.

A Python list intersection contains the items that appear in two or more lists. The best method depends on whether you need speed, stable order, duplicate counts, or support for more than two lists.

For unique common values, convert each list to a set and use set intersection. For output that keeps the first list’s order, use a set for lookup and a list comprehension for the final result. For duplicate-aware results, use collections.Counter.

The official Python documentation covers set operations and Counter.

Before choosing a method, decide what the result should mean. If duplicates do not matter, sets are simple and fast. If the same value should appear more than once in the result, use Counter. If the output should follow the original list, keep a list comprehension in the final step.

This decision is more important than the syntax. Many snippets online show only set(first) & set(second), which is correct for unique membership but not correct for every list task. Lists often carry order, repeated values, or display meaning. A good intersection method should preserve the parts of the input that your program actually needs.

Also consider data size. For small lists, any readable approach is usually fine. For larger lists, avoid nested loops because they check every pair of items. Building one set for lookup keeps the loop simple and usually much faster.

Use Sets For Unique Intersection

The shortest method is to convert both lists to sets and use the & operator.

first = [1, 2, 3, 4]
second = [3, 4, 5, 6]

common = list(set(first) & set(second))

print(common)

This returns the unique common values. The order is not guaranteed because sets are unordered.

Use this form when you only care about membership. It is a good fit for checking shared IDs, tags, options, or categories where duplicates and order do not carry meaning.

If you print the result directly, remember that the display order may look different from the input lists. Sort the result before showing it to users or before comparing it in tests.

Keep The First List Order

Use a set for fast lookup, but build the answer by looping over the first list.

first = ["red", "blue", "green", "red"]
second = ["green", "red", "yellow"]

lookup = set(second)
common = [item for item in first if item in lookup]

print(common)

This keeps the order from first. It also keeps repeated values from first when they are found in second.

This pattern is often the best default for application code because the result remains predictable for users while membership checks stay efficient.

The lookup set is created from the second list, but the comprehension walks through the first list. That means the first list controls output order and duplicate behavior, while the second list controls membership.

Python Pool infographic showing two lists, sets, intersection, and common values
Converting to sets gives concise membership-based intersection for unique values.

Remove Duplicate Output

If you need stable order but each common value should appear once, track what has already been added.

first = ["red", "blue", "green", "red"]
second = ["green", "red", "yellow"]

lookup = set(second)
seen = set()
common = []

for item in first:
    if item in lookup and item not in seen:
        common.append(item)
        seen.add(item)

print(common)

This gives an order-preserving unique intersection.

The extra seen set prevents repeated output while still allowing the loop to follow the first list. That is useful for menus, reports, and display lists.

This pattern is also easy to adjust. You can normalize each item before checking it, skip empty strings, or apply a custom condition inside the loop. Keep those rules close to the append step so the result stays easy to audit.

Keep Duplicate Counts With Counter

Counter is the right tool when duplicate counts matter.

from collections import Counter

first = ["a", "a", "b", "c"]
second = ["a", "b", "b", "d"]

common_counts = Counter(first) & Counter(second)
common = list(common_counts.elements())

print(common)

The & operator keeps the minimum count found in both counters.

In this example, "a" appears once in the result because it appears once in the second list, even though it appears twice in the first list. This is the closest match to a mathematical multiset intersection.

Use this when repeated entries are meaningful, such as inventory counts, votes, or event names that may occur more than once. If repeated entries are accidental noise, remove duplicates before finding the intersection.

Python Pool infographic comparing repeated values, membership, counts, and duplicate-preserving result
A list comprehension or Counter-based method is needed when multiplicity matters.

Intersect More Than Two Lists

Use set.intersection() when several lists need to share a common value.

groups = [
    ["admin", "editor", "viewer"],
    ["editor", "viewer"],
    ["owner", "viewer"],
]

common = set(groups[0]).intersection(*map(set, groups[1:]))

print(common)

This returns the values present in every list.

Handle empty input separately in production code. If there are no lists, there is no first set to start from.

For a large group of lists, this approach is easier to read than chaining many & operators. It also makes it simple to prepare each input list in the same way before intersecting them.

Wrap The Ordered Method In A Function

A small function makes the order-preserving approach easy to reuse.

def list_intersection(first, second):
    lookup = set(second)
    seen = set()
    result = []

    for item in first:
        if item in lookup and item not in seen:
            result.append(item)
            seen.add(item)

    return result

print(list_intersection([3, 1, 2, 3], [2, 3, 4]))

This function returns common values in the same order they first appear in first.

Choose the method based on the contract you want: sets for unique membership, a list comprehension for order, seen for order plus uniqueness, and Counter for duplicate counts. Naming that contract in code reviews helps avoid subtle changes in output behavior later.

Choose Unique Membership

set(first) & set(second) answers whether a value appears in both lists. It is concise and efficient for unique results, but set order should not be treated as the original list order.

Python Pool infographic mapping a first list through membership checks to ordered common values
Filter the first list when its order should define the output order.

Preserve Order

Build a lookup set from the second list and filter the first list when the output must follow the first input. Decide whether repeated matches should remain repeated or be deduplicated.

Keep Duplicate Counts

Counter treats each list as a multiset. Its intersection keeps the smaller count for every shared value, which is the right model for inventory or token occurrences.

Python Pool infographic testing empty lists, unhashable values, duplicates, order, and validation
Check hashability, duplicate policy, order preservation, empty inputs, and equality semantics.

Handle More Than Two Lists

For unique values, intersect sets across every input. For ordered or duplicate-aware results, choose a reference list and apply the same membership or count policy consistently.

Test The Contract

Cover empty lists, duplicates, different order, unhashable values, multiple inputs, and values that compare equal. The correct result is defined by order and multiplicity as much as by membership.

Python’s set operations and Counter documentation define the main approaches. Related references include membership checks, dynamic data, and collection tests.

For related collection choices, compare membership checks, dynamic data, and collection tests when defining order and duplicate rules.

Frequently Asked Questions

How do I find the intersection of two Python lists?

Use set(first) & set(second) for unique common values, or a different method when order or duplicates matter.

How do I preserve list order?

Filter the first list with a set built from the second list, while deciding how repeated values should behave.

How do I keep duplicates?

Use collections.Counter to compute multiset intersection when each occurrence count matters.

How do I intersect more than two lists?

Intersect sets for unique values or apply a duplicate-aware strategy across every input with an explicit order policy.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted