Symmetric Difference of Two Dicts’ Keys

Medium ⏱ 12 min 61% acceptance ★★★★☆ 4.2
Write a function keys_not_shared(d1, d2) that returns a sorted list of keys that appear in exactly one of d1 or d2, but not both (the symmetric difference of their key sets).

Examples

Example 1
Input
keys_not_shared({'a': 1, 'b': 2, 'c': 3}, {'b': 9, 'c': 8, 'd': 7})
Output
['a', 'd']
Explanation

'a' is only in d1, 'd' is only in d2; 'b' and 'c' are shared and excluded.

Constraints

  • Result must be sorted.

Topics

Dictionariesset operations

Companies

GoogleMeta

Hints

Hint 1

`set(d1.keys()) ^ set(d2.keys())` is the symmetric difference operator.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.