0

I have some calculation results, which I'm parsing and sorting with a dictionary, the expected sorted dictinary item should look like

key: [[1,2,3,4]]

But it also may happen that it looks like

key: [[1,2,3,4],[5,6,7,8]]

In such a case I need to sum the values on the same places, to get like

key: [[6,8,10,12]]

How can I do that with Python, supposing that I'm not sure how many data sets would occur instead of one, but I need to sum them in a described way in any case?

yxfxmx
  • 665
  • 1
  • 6
  • 13

1 Answers1

3

You could use zip and unpacking:

>>> [sum(i) for i in zip(*key)]
[6, 8, 10, 12]
fredtantini
  • 15,966
  • 8
  • 49
  • 55