pythonpython-3.xdefaultdict

Mixing defaultdict (dict and int) to get nested default dictionary with sum of values


I have 2 example lists and what I want to achieve is to obtain a nested default dictionary with the sum of the values.

The following code works nice:

from collections import defaultdict

l1 = [1,2,3,4]
l2 = [5,6,7,8]
dd = defaultdict(int)

for i in l1:
    for ii in l2:
        dd[i] += ii

but what I'm trying to do is to create a default key in the d dictionary:

from collections import defaultdict

l1 = [1,2,3,4]
l2 = [5,6,7,8]
dd = defaultdict(int)

for i in l1:
    for ii in l2:
        dd[i]['mykey'] += ii

and this throws an error:

Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 12, in <module>
TypeError: 'int' object is not subscriptable

Basically what I'm not able to understand is if there is the chance to mix defaultdict(dict) and defaultdict(int).


Solution

  • The defaultdict data structure receives a function that will supply the default value, so if you want to create a defautdict(int) as default value provide a function that does that, for example lambda : defaultdict(int):

    from collections import defaultdict
    from pprint import pprint
    
    l1 = [1, 2, 3, 4]
    
    l2 = [5, 6, 7, 8]
    
    dd = defaultdict(lambda : defaultdict(int))
    
    for i in l1:
    
        for ii in l2:
            dd[i]['mykey'] += ii
    
    
    pprint(dd)
    

    Output

    defaultdict(<function <lambda> at 0x7efc74d78f28>,
                {1: defaultdict(<class 'int'>, {'mykey': 26}),
                 2: defaultdict(<class 'int'>, {'mykey': 26}),
                 3: defaultdict(<class 'int'>, {'mykey': 26}),
                 4: defaultdict(<class 'int'>, {'mykey': 26})})