Introduction
In Python, the symmetric difference between two sets is the set of elements that are in either of the sets, but not in their intersection. In this challenge, you will write a function that takes two lists as input and returns their symmetric difference.
Symmetric Difference
Write a function symmetric_difference(a, b) that takes two lists as arguments and returns their symmetric difference as a list. The function should not filter out duplicate values.
To solve this problem, you can follow these steps:
- 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.
- Concatenate the two lists obtained in step 2.
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]
Summary
In this challenge, you have learned how to find the symmetric difference between two lists in Python. You have also learned how to use sets and list comprehensions to solve this problem.