1

Is there a way to check if two lists are equal, depending on the values in it but regardless of their order.

for example:

[3,4,5] == [5,3,4] will be: true

You can of course sort the 2 lists and then compare them, the question is whether it is possible to check if lists are equal using their values, without sorting them

Elicon
  • 206
  • 1
  • 11

2 Answers2

7

You can use Counter from collections

Counter(listA) == Counter(listB)

or if you want to sort them

sorted(listA) == sorted(listB)
2

If you don't want to sort, then compare the quantities of the present elements of each in the form of a dictionary using Counter from python's collections library.

from collections import Counter

def check_equal_without_sort(arr1, arr2):
    return Counter(arr1) == Counter(arr2)

arr1 = [3,4,5]
arr2 = [5,3,4]
print(check_equal_without_sort(arr1,arr2))
Gautam Chettiar
  • 449
  • 2
  • 11