Dictionary Map
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.
Define a function dictmap(d, f)
which takes a dictionary
d
and a function f
as input, and updates d
by replacing each value val
in the dictionary with f(val)
,
leaving the keys unchanged. Your function should modify d
but should not return anything explicitly (i.e. it should return None
implicitly by lacking a return statement). You may assume that all of the values
in the dictionary are the proper type to be accepted as input into the
given function.
For example, consider this code:
dictionary = {"a":10, "b":-2}
square = lambda x: x**2
out = dictmap(dictionary, square)
# out should be None, but dictionary should now be {"a":100, "b":4}
Relevant Readings about using functions as arguments.
No file selected
Next Exercise: Caesar's Cipher