find the symmetric difference of two maps in python

You can find the symmetric difference of two maps in Python by converting the maps to sets and finding the symmetric difference of the sets.

main.py
map1 = {"a": 1, "b": 2, "c": 3}
map2 = {"b": 2, "c": 4, "d": 5}

set1 = set(map1.items())
set2 = set(map2.items())

symmetric_difference = set1.symmetric_difference(set2)
171 chars
8 lines

In this example, the symmetric difference between map1 and map2 is a set that contains the key-value pairs ("a", 1), ("c", 3), and ("d", 5).

Note that when converting a map to a set, you need to use the items() method to get a set of key-value pairs.

gistlibby LogSnag