Reverse

The questions below are due on Thursday June 24, 2021; 11:00:00 AM.
 
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 code which reverses all the key-value pairs in a dictionary d. 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 we want to print 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.

Your code should set up, on the first line, a variable named d to store the dictionary. It should print the reversed dictionary.

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