Question : python compare lists unordered
Answered by : anxious-axolotl
def compare(s, t): return sorted(s) == sorted(t)
Source : https://stackoverflow.com/questions/7828867/how-to-efficiently-compare-two-unordered-lists-not-sets-in-python | Last Update : Wed, 20 Jan 21
Question : how to compare two unordered lists in python
Answered by : liad
# 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
Source : https://stackoverflow.com/questions/7828867/how-to-efficiently-compare-two-unordered-lists-not-sets | Last Update : Mon, 18 Jul 22