Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value.
dict.items()
to iterate over the dictionary, assigning the values produced by fn
to each key of a new dictionary.def map_values(obj, fn):
return dict((k, fn(v)) for k, v in obj.items())
users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
}
map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1}
Python, Dictionary
Combines two or more dictionaries, creating a list of values for each key.
Python, Dictionary
Finds all keys in the provided dictionary that have the given value.
Python, Dictionary
Finds the first key in the provided dictionary that has the given value.