Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Files

Latest commit

 

History

History
23 lines (19 loc) · 606 Bytes

symmetric-difference.md

File metadata and controls

23 lines (19 loc) · 606 Bytes
title type tags cover dateModified
List symmetric difference
snippet
list
ice
2020-11-02 19:28:35 +0200

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]