Menu

gitpiper

symmetric_difference python Code Snippet in 2024

listintermediate

Last Updated: 9 April 2024

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]

python snippet similar to symmetric_difference For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️