I have a function for getting an item from a dict:
def dict_lookup(dictionary, key):
return dictionary[key]
I'll be mapping this function via multiprocessing to look up values from a series of very long dicts:
dictlist = [{}, {}, {}]
dict_lookup_partial = functools.partial(dict_lookup, key=some_key)
with multiprocessing.Pool() as pool:
values = pool.map(dict_lookup_partial, dictlist)
I feel like I shouldn't have to define a function for getting a value from a dict. Is there some inbuilt way to do this?
Write a Python program to map the values of a given list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value. Use map() to apply fn to each value of the list. Use zip() to pair original values to the values produced by fn.
It works by creating a new dictionary which contains every value from d and mapped to a new key. The key is retrieved like this: m[key] if m in key else key , but then with the default . get function (which supports default values if the key doesn't exist).
Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable (tuple, lists, etc.). It is used when you want to apply a single transformation function to all the iterable elements. The iterable and function are passed as arguments to the map in Python.
The itemgetter function from the standard library's operator module provides this behaviour:
>>> import multiprocessing as mp
>>> import operator
>>> dictlist = [{'a': 1, 'b':2, 'c': 10}, {'a': 3, 'b': 4, 'c': 20},
{'a': 5, 'b': 6, 'c': 30}]
>>> agetter = operator.itemgetter('a')
>>> with mp.Pool() as pool:
... avalues = pool.map(agetter, dictlist)
...
>>> avalues
[1, 3, 5]
It can also be used to retrieve values for multiple keys:
>>> bcgetter = operator.itemgetter('b', 'c')
>>> with mp.Pool() as pool:
... bcvalues = pool.map(bcgetter, dictlist)
...
>>> bcvalues
[(2, 10), (4, 20), (6, 30)]
In general, the operator module is the first place to look for a function that replicates a builtin's behaviour for use in map
, filter
or reduce
.
There is dict.__getitem__
function. It's called everytime you access the dictionary value through square brackets like this: dictionary[key]
, but you can call it explicitly.
>>> d = {"foo": "bar"}
>>> dict.__getitem__(d, "foo")
"bar"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With