There is no such function; the easiest way to do this is to use a dict comprehension:
my_dictionary = {k: f(v) for k, v in my_dictionary.items()}
Note that there is no such method on lists either; you'd have to use a list comprehension or the map() function.
As such, you could use the map() function for processing your dict as well:
my_dictionary = dict(map(lambda kv: (kv[0], f(kv[1])), my_dictionary.items()))
but that's not that readable, really.
(Note that if you're still using Python 2.7, you should use the .iteritems() method instead of .items() to save memory. Also, the dict comprehension syntax wasn't introduced until Python 2.7.)
There is no such function; the easiest way to do this is to use a dict comprehension:
my_dictionary = {k: f(v) for k, v in my_dictionary.items()}
Note that there is no such method on lists either; you'd have to use a list comprehension or the map() function.
As such, you could use the map() function for processing your dict as well:
my_dictionary = dict(map(lambda kv: (kv[0], f(kv[1])), my_dictionary.items()))
but that's not that readable, really.
(Note that if you're still using Python 2.7, you should use the .iteritems() method instead of .items() to save memory. Also, the dict comprehension syntax wasn't introduced until Python 2.7.)
These toolz are great for this kind of simple yet repetitive logic.
http://toolz.readthedocs.org/en/latest/api.html#toolz.dicttoolz.valmap
Gets you right where you want to be.
import toolz
def f(x):
return x+1
toolz.valmap(f, my_list)
Videos
Hi all,
Working my way through a course on Data Structures right now and am beginning to learn about hash maps. Can someone explain to me what the difference is between a hash map and a regular, old dictionary? It seems like they both have a key:value pair and store that paired information, except the hash map seems way more complicated due to hash collisions and compression, etc, etc. The only thing I can think of is that it has something to do with efficiency? A hash map is more easily searchable and/or returns values faster?
Looking forward to replies. Thanks.
I am reading Python Cookbook. And here in this chapter https://learning.oreilly.com/library/view/python-cookbook/0596001673/ch01s06.html, author explain ways to implement one to many mapping in dictionaries.
Approach 1: Allow duplicacy.
d = { key1: [val1, val2...], key2: [val3, val4,...], ...}
Approach 2: For uniqueness.
d = { key1: { val1: 1, val2: 1, ...}, key2: { val3: 1, val4: 1,....} }
Why don't author suggests key: set() pair or the above 2nd approach is more suitable. Am I missing something here?