Invert a Dictionary

easypy_datastructures

Implement revert(dct).

Invert a {key: value} mapping into {value: [keys...]}: each value becomes a key whose list holds every original key that mapped to it. Don't modify the input. For example revert({"a": "1", "b": "2", "c": "1"}){"1": ["a", "c"], "2": ["b"]} (key order within a list doesn't matter). Do it in O(n) — a single pass, not O(n^2).

Your solution

Edit revert and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

Walk the items once; for each (key, value) append key to the list at out[value] (dict.setdefault keeps it one pass).