6

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?

wim
  • 338,267
  • 99
  • 616
  • 750
snazzybouche
  • 2,241
  • 3
  • 21
  • 51
  • 1
    This "function for getting an item from a dict" is called [`itemgetter`](https://docs.python.org/3/library/operator.html#operator.itemgetter). – wim Nov 29 '19 at 06:52

2 Answers2

4

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.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
1

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"
irezwi
  • 789
  • 8
  • 14
  • Nice! Sadly it looks like `functools.partial` can't handle filling in the 2nd positional parameter of builtins - https://stackoverflow.com/questions/7811247/how-to-fill-specific-positional-arguments-with-partial-in-python - so I'd need a wrapper function anyway, – snazzybouche Nov 28 '19 at 14:34