Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map dict lookup

Tags:

python

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?

like image 487
snazzybouche Avatar asked Nov 28 '19 12:11

snazzybouche


People also ask

How do you map a dictionary in Python?

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.

How do I map a value from one dictionary to another in Python?

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).

What is map function in Python?

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.


2 Answers

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.

like image 148
snakecharmerb Avatar answered Oct 09 '22 11:10

snakecharmerb


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"
like image 1
irezwi Avatar answered Oct 09 '22 12:10

irezwi