What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
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:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
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:
Rank #2
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:
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →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.
Recommended Free Tools
Best Value
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.
Quick Recap
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
Counterwhen 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.




