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