Files
30-seconds-of-code/snippets/symmetric-difference.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

626 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
List symmetric difference list ice 2019-08-21T08:37:04+03:00 2020-11-02T19:28:35+02:00

Returns the symmetric difference between two iterables, without filtering out duplicate values.

  • Create a set from each list.
  • Use a list comprehension on each of them to only keep values not contained in the previously created set of the other.
def symmetric_difference(a, b):
  (_a, _b) = (set(a), set(b))
  return [item for item in a if item not in _b] + [item for item in b
          if item not in _a]
symmetric_difference([1, 2, 3], [1, 2, 4]) # [3, 4]