I need to create a dictionary from a list of integers, where each key contains the value of the sums that are in the list:
t = [[1,2],[2,1],[3,1],[4,1],[1,1],[2,2],[1,2]]
dicionario = {}
for i in t:
dicionario[str(i[0])] = 0
for i in t:
dicionario[str(i[0])] += i[1]
Where my dictionary would have the result: {'1':5,'2':3,'3':1,'4':1}
But I'm doing two loops as you can see, but how do I make this task as fast as possible, ie with just one loop how would I do it?
t = [[1,2],[2,1],[3,1],[4,1],[1,1],[2,2],[1,2]]
dicionario = {}
for i in t:
dicionario[str(i[0])] += i[1]
KeyError Traceback (most recent call last)
<ipython-input-8-8eda406a14d0> in <module>()
1 for i in t:
----> 2 dicionario[str(i[0])] += i[1]
3
KeyError: '1'