I have a dictionary like below:
d = [{152: 1}, {151: 3}, {152: 4}, {153: 5}, {153: 10}, {154: -0.1}]
I want to sort them like below:
{151: 3, 152: 5, 153: 15, 154: -0.1}
I tried the below by following this answer ...
sorted = dict(functools.reduce(operator.add, map(collections.Counter, d)))
The output I got is :
{151: 3, 152: 5, 153: 15}
I do not understand why functools.reduce(operator.add, )
removes the {154: -0.1}
. Isn't operator.add
just a plain summation method?
operator.add(a, b)
operator.add(a, b)
Return a + b, for a and b numbers.
Questions
- What could be the workaround to achieve the desired output?
- Why does
functools.reduce(operator.add, )
remove the negative value?