Skip to main content
added 269 characters in body
Source Link
Alain T.
  • 42.2k
  • 4
  • 36
  • 57

If you don't have duplicates in either lists you can use a set:

if listA==listBlistA == listB  \
or listA and listB \
   and len(listA) == len(listB) \
   and not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if listA==listBlistA == listB  \
or listA and listB \
   and len(listA) == len(listB) \
   and Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences

If you don't have duplicates in either lists you can use a set:

if listA==listB or len(listA) == len(listB) \
and not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if listA==listB or len(listA) == len(listB) \
and Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences

If you don't have duplicates in either lists you can use a set:

if listA == listB  \
or listA and listB \
   and len(listA) == len(listB) \
   and not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if listA == listB  \
or listA and listB \
   and len(listA) == len(listB) \
   and Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences
added 269 characters in body
Source Link
Alain T.
  • 42.2k
  • 4
  • 36
  • 57

If you don't have duplicates in either lists you can use a set:

if listA==listB or len(listA) == len(listB) \
and not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if listA==listB or len(listA) == len(listB) \
and Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences

If you don't have duplicates in either lists you can use a set:

if not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences

If you don't have duplicates in either lists you can use a set:

if listA==listB or len(listA) == len(listB) \
and not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if listA==listB or len(listA) == len(listB) \
and Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences
Source Link
Alain T.
  • 42.2k
  • 4
  • 36
  • 57

If you don't have duplicates in either lists you can use a set:

if not set(listA).symmetric_difference(listB):
   # lists have the same elements
else:
   # there are differences

If you do allow duplicates, then yo can use Counter from collections (which would also work if you don't have duplicates)

from collections import Counter

if Counter(listA)==Counter(listB):
   # lists have the same elements
else:
   # there are differences