StackOverflow

### Answer rating: 303

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

a & b should be considered equal, because they have exactly the same elements, only in different order.

The thing is, my actual lists will consist of objects (my class instances), not integers.

**O(n)**: The *Counter()* method is best (if your objects are hashable):

```
def compare(s, t):
return Counter(s) == Counter(t)
```

**O(n log n)**: The *sorted()* method is next best (if your objects are orderable):

```
def compare(s, t):
return sorted(s) == sorted(t)
```

**O(n * n)**: If the objects are neither hashable, nor orderable, you can use equality:

```
def compare(s, t):
t = list(t) # make a mutable copy
try:
for elem in s:
t.remove(elem)
except ValueError:
return False
return not t
```

I remember one day, when I was about 15, my little cousin had come over. Being the good elder sister that I was, I spent time with her outside in the garden, while all the adults were inside having a ...

23/09/2020

Efficiently perform data collection, wrangling, analysis, and visualization using Python. Recent advancements in computing and artificial intelligence have completely changed the way we understand ...

23/09/2020

Pandas 1.x Cookbook: Practical recipes for scientific computing, time series analysis, and exploratory data analysis using Python, 2nd Edition....

05/09/2021

This book contains chapters authored by several leading experts in the field of cloud computing. The book is presented in a coordinated and integrated manner starting with the fundamentals and followe...

10/07/2020

X
# Submit new EBook