I think I want to make a 2D dictionary with multiple keys per value.
I know how to make a 2D dictionary using defaultdict
:
from collections import defaultdict
2d_dict = defaultdict(dict)
2d_dict['canned_food']['spam'] = 'delicious'
And I know that using regular dictionaries you can make with multiple keys like:
dictionary={('food','canned_food'):spam}
But I want to do something like lookup by tuple-of-keys:
2d_dict[('canned_food','food')]['spam'] = 'delicious'
In the first dimension of dictionary I need ~25 keys per value. Is there a way to do this with defaultdict
?
Even if there is a way to do it with dicts is this a reasonable way to make a simple multidimensional lookup table?
In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.
In Python, we can add multiple key-value pairs to an existing dictionary. This is achieved by using the update() method. This method takes an argument of type dict or any iterable that has the length of two - like ((key1, value1),) , and updates the dictionary with new key-value pairs.
General Idea: In Python, if we want a dictionary to have multiple values for a single key, we need to store these values in their own container within the dictionary. To do so, we need to use a container as a value and add our multiple values to that container. Common containers are lists, tuples, and sets.
This question already has answers here:You can't. Keys have to be unique.
Apart from 2d_dict
being an invalid variable name (it starts with a digit), your existing solution already works:
>>> from collections import defaultdict
>>> d2_dict = defaultdict(dict)
>>> d2_dict[('canned_food', 'food')]['spam'] = 'delicious'
>>> d2_dict
defaultdict(<type 'dict'>, {('canned_food', 'food'): {'spam': 'delicious'}})
In fact, you don't even need the parentheses - Python will still recognise your key as a tuple:
>>> d2_dict['fresh_food', 'food']['eggs'] = 'delicious'
>>> d2_dict
defaultdict(<type 'dict'>, {('canned_food', 'food'): {'spam': 'delicious'},
('fresh_food', 'food'): {'eggs': 'delicious'}})
... and, yes, it's a perfectly reasonable way to build a 2D+1D lookup table.
If you want to build a 3D lookup table using nested dicts instead of tuple keys, this works:
>>> d3_dict = defaultdict(lambda: defaultdict(dict))
>>> d3_dict['dried_food']['food']['jerky'] = 'chewy'
>>> d3_dict
defaultdict(<function <lambda> at 0x7f20af38a2a8>,
{'dried_food': defaultdict(<type 'dict'>, {'food': {'jerky': 'chewy'}})})
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