Invert a Dictionary

Medium ⏱ 12 min 50% acceptance ★★★★☆ 4.3
Write a function invert(d) that returns a new dict where each original value becomes a key and its original key becomes the value. Assume all values in d are unique and hashable.

Examples

Example 1
Input
invert({'a': 1, 'b': 2, 'c': 3})
Output
{1: 'a', 2: 'b', 3: 'c'}
Explanation

Swap key/value pairs.

Constraints

  • Values are guaranteed unique.
  • Values are guaranteed hashable.

Topics

Dictionariesinversion

Companies

UberPayPal

Hints

Hint 1

A dict comprehension: `{v: k for k, v in d.items()}`.

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