Determine if 2 lists have the same elements, regardless of order?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To decide whether two Python lists contain the same elements regardless of order, you first have to answer one question: do duplicates matter. The best solution changes depending on whether [1, 1, 2] should count as the same as [1, 2, 2].
If Duplicates Matter, Use Counter
When element counts matter, collections.Counter is usually the cleanest approach.
This works because Counter compares both the unique values and how many times each value appears.
It also correctly rejects lists that have the same unique elements but different frequencies:
If Duplicates Do Not Matter, Use set
If you only care about membership and do not care how many times an element appears, compare sets:
This is a different question from multiset equality. set discards duplicates, so use it only when duplicate counts are irrelevant to the problem.
Sorting Also Works for Comparable Elements
Another common solution is sorting both lists and comparing the results:
This works well when the elements are sortable and you do care about duplicates. It is often easy to read, though Counter is usually more explicit about the real intent.
What About Unhashable Elements
Counter and set need hashable elements. If your lists contain inner lists or dictionaries, those structures cannot go directly into a set or counter.
In that case, sorting may still work if the elements are comparable, or you may need to convert them into hashable representations first.
For example:
This works because lists of integers can be compared lexicographically in Python.
For nested dictionaries or more complex objects, you often need a preprocessing step that converts each item into a stable comparable form before any equality strategy can work reliably.
Choose the Method by Meaning, Not Habit
A practical rule is:
- use
Counterfor multiset equality - use
setfor unique-element equality - use
sortedwhen you want a simple readable comparison and the data is sortable
Picking the right method is about semantics, not only performance.
It also makes the code easier for other developers to read later, because the chosen tool signals whether duplicate counts are part of the requirement.
Common Pitfalls
The biggest pitfall is using set when duplicates matter. That silently changes the question being asked.
Another issue is using sorted on values that are not mutually comparable, such as a list mixing strings and numbers in Python 3.
People also forget about unhashable items when reaching for Counter or set. If the elements are lists, dictionaries, or other mutable containers, those tools may fail immediately.
Summary
- Use
Counter(a) == Counter(b)when order does not matter but duplicate counts do. - Use
set(a) == set(b)only when duplicates are irrelevant. - Use
sorted(a) == sorted(b)when the elements are sortable and you want a simple direct comparison. - Be careful with unhashable elements when using
Counterorset. - Pick the comparison method based on what "same elements" actually means in your problem.
Related reading
- Determine if a binary tree is subtree of another binary tree using pre-order and in-order strings
- determine if a point sits inside an arbitrary shape?
- Determine if more than half of the array repeats in a distinct array
- Determine if the solution can be optimally given using greedy algorithm
- Determine if Json results is object or array
- Determine the most common occurrence in an array
- Determine if variable is defined in Python
- Determine the type of an object?

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.