DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

Compare Two Lists in Python: Non-Matches, Duplicates, and Order

Python’s ==, set operations, and Counter answer different list-comparison questions. Choose based on whether order, duplicate counts, and output order matter.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the comparison that matches what you mean by “same”: use == for identical values in identical positions, set operations for unique membership differences, and collections.Counter when counts matter but order does not. If the result must follow an input list’s order, iterate that list rather than returning a set.

How do I compare two lists in Python?

First decide whether order and repeated values are meaningful. These comparisons answer different questions, so there is no single general-purpose list-difference operation.

What you want to know Use Duplicates matter? Order matters?
Are the lists identical, including positions? a == b Yes Yes
Do they contain the same distinct values? set(a) == set(b) No No
Do they contain the same values the same number of times? Counter(a) == Counter(b) Yes No
Which distinct values in a are absent from b? set(a) - set(b) No No
Which values from a are absent from b, retaining source order? Iterate a and check membership in set(b) Depends on the filtering rule Yes

How do I test whether two lists are exactly equal?

Use the equality operator:

a = [1, 2, 2]
b = [1, 2, 2]
c = [2, 1, 2]

print(a == b)  # True
print(a == c)  # False

Python sequence equality compares corresponding elements, and the sequences must have the same length and sequence type. Consequently, order and repeated occurrences are both part of the comparison: swapping two values or adding an extra copy makes the lists unequal. See the Python 3.11 expressions reference.

How do I find values in one list but not another?

For a unique membership difference, convert both lists to sets and subtract:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
a = ["red", "blue", "blue", "green"]
b = ["blue", "yellow"]

only_in_a = set(a) - set(b)
print(only_in_a)  # {'red', 'green'}

This is one-way difference: it returns distinct values found in a but not b. For values unique to either list, use symmetric difference, set(a) ^ set(b). Set operations discard repeated occurrences and do not preserve list order, so a set result is not a general-purpose ordered list diff. Python documents set behavior and operations in its built-in types reference.

How do I compare lists without ignoring duplicates?

Use Counter when order is irrelevant but the number of occurrences matters:

from collections import Counter

a = [1, 2, 2]
b = [2, 1, 1]

print(Counter(a) == Counter(b))  # False

The lists have the same distinct values, but their counts differ: a contains two 2s, while b contains two 1s. A counter records each hashable element’s frequency, so equality compares those frequencies without considering positions.

To inspect extra occurrences on each side, subtract the counters:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from collections import Counter

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

print(Counter(a) - Counter(b))  # Counter({'b': 1, 'c': 1})
print(Counter(b) - Counter(a))  # Counter({'d': 1})

These subtraction results show positive count differences. They are not ordered lists of every unmatched input position. Counter equality treats missing keys as zero-count keys in Python 3.10 and later; see the CPython collections documentation.

How do I keep the original order?

Iterate the list whose order you want to retain, using a set for fast membership checks. The following version keeps every occurrence from a if its value is absent from b:

a = ["red", "blue", "red", "green"]
b = ["blue"]

b_values = set(b)
only_in_a_in_order = [value for value in a if value not in b_values]
print(only_in_a_in_order)  # ['red', 'red', 'green']

If instead you want each unmatched value only once, while retaining the order of its first appearance in a, track what has already been emitted:

a = ["red", "blue", "red", "green"]
b_values = {"blue"}
seen = set()
only_unique_in_a_ordered = []

for value in a:
    if value not in b_values and value not in seen:
        only_unique_in_a_ordered.append(value)
        seen.add(value)

print(only_unique_in_a_ordered)  # ['red', 'green']

The choice between these two filters is about the output you need: one preserves unmatched occurrences, the other preserves only each value’s first unmatched appearance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What if the lists contain nested or unhashable values?

Sets and counters use elements as hashable keys. A list containing inner lists or dictionaries therefore cannot be passed directly to set() or Counter(). Direct list equality can still compare corresponding nested values:

a = [[1, 2], {"name": "Ada"}]
b = [[1, 2], {"name": "Ada"}]

print(a == b)  # True

For order-independent comparison of nested data, decide which fields define identity and derive a hashable key or canonical representation for each item. That transformation is part of the comparison rule: for example, ignoring a dictionary field or sorting nested values can make objects compare equal that would not otherwise be treated as interchangeable. Do not use a conversion unless it reflects the meaning of equality in your data.

Which method should I choose?

  • Use == when position, length, values, and duplicates must all match.
  • Use sets when you care only about distinct membership and the elements are hashable.
  • Use Counter when order can differ but occurrence counts must match.
  • For ordered non-matches, iterate the source list and explicitly choose whether to retain repeated unmatched values.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.