Dictionary Swap
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 program that swaps two keys of a dictionary when supplied with a dictionary d
, and two keys k1
and k2
. You may assume that your program will only be run with dictionaries that contain both specified keys.
The first lines of your programs should set up three variables to hold the inputs specified above. For example:
d = {'dog': 'canine', 'cat': 'feline'}
k1 = 'dog'
k2 = 'cat'
Your program should print the dictionary after the resulting swap. The exmaple above should print:
{'dog':'feline', 'cat':'canine'}
Note, the order of keys in the dictionary is not enforced; it's also ok to print {'cat':'canine', 'dog':'feline'}
. (This problem may feel familiar from tutorial!)
Relevant Readings - change mappings
No file selected
Next Exercise: Reverse