Reverse

The questions below are due on Sunday June 30, 2024; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Write a function dict_reverse that returns a new dictionary with all the key-value pairs in the input dictionary reversed. That is, if there is a key "foo" in d with corresponding value 1, then there is a key 1 with corresponding value "foo" in the reversed dictionary. For a bigger example, if our input dictionary d looks like:

d = {'foo': 1, 'bar': 2, 'baz': 3}

Then dict_reverse(d) will return a dictionary that looks like:

{1: 'foo', 2: 'bar', 3: 'baz'}

For the purposes of this exercise, you can assume that any input dictionary has unique values, and each value is of a type that can be used as a dictionary key.

Relevant Readings - reverse lookup is somewhat relevant, but instead of looking up keys using values, you need to construct a new dictionary.

  No file selected

Next Exercise: Removing Duplicates

Back to exercises