The function below should return a new dictionary which has summed the values up.
import functools
def sumUp(d):
for k in d:
d.update({k: functools.reduce(lambda x, y: x + y, d[k])})
print(d)
When i call the function as follows i get the following TypeError
which i can't understand why:
sumUp({"Ungefucht": (165, 165, 165, 255, 286.25, 255, 165, 240, 240, 150), "Malsch": (120, 240, 120, 120, 120, 120, 120), "AAA": (1, 2), "Fens": (115.20, 69.60, 28.80, 50.40), "Betti": (82.50,)})
Traceback (most recent call last):
File "", line 1, in
File "/home/amir/programming/python/lern.py", line 6, in sumUp print(d)
TypeError: reduce() arg 2 must support iteration
When i omit one of the key-values it works fine:
sumUp({"Ungefucht": (165, 165, 165, 255, 286.25, 255, 165, 240, 240, 150), "Malsch": (120, 240, 120, 120, 120, 120, 120), "AAA": (1, 2), "Fens": (115.20, 69.60, 28.80, 50.40)})
{'Malsch': 960, 'Ungefucht': 2086.25, 'Fens': 264.0, 'AAA': 3}
Why is the first example with one more item not working as expected?