Explain Codes LogoExplain Codes Logo

Check if two unordered lists are equal

python
collections
counter
hashmap
Alex KataevbyAlex Kataev·Feb 1, 2025
TLDR

Let's start by using collections.Counter to compare unordered lists:

from collections import Counter # Is same pile? Let's see. equal = Counter(list1) == Counter(list2) # Higher chance than getting all heads on 10 coin flips! # Example: equal = Counter([1, 2, 3]) == Counter([3, 2, 1]) # True, we're partying like it's 1999!

collections.Counter treats lists as unordered collections, which means the order doesn't affect comparison results.

Cutting through the duplicates jungle

Python set() does handle unordered lists, but it fails when you have duplicates because it only cares about unique items:

equal_with_set = set(['apple', 'apple', 'banana']) == set(['banana', 'apple']) # False, because set() neglects the 2nd apple. Poor apple!

Therefore, to include duplicates in our equality-check expedition, we'll stick to collections.Counter(), which counts the unique occurrences of items:

from collections import Counter equal = Counter(['apple', 'apple', 'banana']) == Counter(['banana', 'apple', 'apple']) # True, because Counter() cares for each apple. Yes we love apples!

It's all about order: Sorted lists comparison

Are you interested in permutations and need to respect the order as well? Python got you covered with the sorted() function:

equal = sorted(list1) == sorted(list2) # Example: equal = sorted(['apple', 'banana', 'apple']) == sorted(['banana', 'apple', 'apple']) # True, sorted lists are like well-ordered soldiers in a row.

But remember, calling sorted() is like calling your mom. Always include it when order matters!

Coding sanely: Efficiency matters

When dealing with long lists on a foggy night, make sure that your torch has enough charge! Also, make sure that your pythonic machine is efficient enough. A quick length check can save you the trouble of unnecessary computations:

# A quick inequality fast-track if len(list1) != len(list2): equal = False else: equal = Counter(list1) == Counter(list2)

Troubleshooting: When things go down

Dancing with non-hashable elements

collections.Counter() and set() are primed for hashable elements. Unhashable elements (like lists or dictionaries) are the party crashers. So be prepared:

  • Transform the elements to make them hashable (e.g., convert sublists to tuples)
  • Go ninja and implement a custom comparison which iterates through the lists and compares elements one by one.

Getting the hang of complexity

Python is high, but computational complexity can make it look like a joke. Comparisons with Counter need O(N), and creation needs O(N) too. On the other hand, sorting requires O(N log N) quacks in your time duck.

Bottom line? Measure and then decide. Choose your side, Luke!

Wanna play (with) custom?

When dealing with lists of custom objects, the gym rules apply: __eq__ and __hash__ methods need to be fit for a fruitful comparison session. They are akin to the genes for equivalent metaphorical workout results.