Python
List Comparison
Duplicates
Algorithm
Sorting

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.

Practice algorithms

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.

python
1from collections import Counter
2
3a = [1, 2, 2, 3]
4b = [3, 2, 1, 2]
5
6print(Counter(a) == Counter(b))  # True

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:

python
1from collections import Counter
2
3a = [1, 1, 2]
4b = [1, 2, 2]
5
6print(Counter(a) == Counter(b))  # False

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:

python
1a = [1, 2, 2, 3]
2b = [3, 1, 2]
3
4print(set(a) == set(b))  # True

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:

python
1a = [3, 1, 2, 2]
2b = [2, 3, 2, 1]
3
4print(sorted(a) == sorted(b))  # True

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:

python
1a = [[1, 2], [3, 4]]
2b = [[3, 4], [1, 2]]
3
4print(sorted(a) == sorted(b))  # True

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 Counter for multiset equality
  • use set for unique-element equality
  • use sorted when 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 Counter or set.
  • Pick the comparison method based on what "same elements" actually means in your problem.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms