I need code to sum lists. Example: [[1,2,3],[1,2,3]]. answer be like [2,4,6]. [[1,1,1],[2,2,2],[3,3,3]] answer: [6,6,6]
Asked
Active
Viewed 37 times
0
-
Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/261592, and try to find solutions yourself before asking, for example by [using a search engine](https://duckduckgo.com/?q=python+sum+list+of+lists). – Karl Knechtel Jun 26 '22 at 23:51
2 Answers
0
Refer this this can work for n
number of list too and please try first, read docs, and provide relevant resources that you tried or code snippet.
[a+b+c for a,b,c in zip([1,1,1],[2,2,2],[3,3,3])]
Output: [6,6,6]
if you are okay with using packages use numpy and apply it but read it's documentation beforehand.

GodWin1100
- 1,380
- 1
- 6
- 14
0
Perform a transpose operation, and then use sum()
:
lst = [[1,2,3],[1,2,3]]
[sum(sublist) for sublist in zip(*lst)]
This outputs:
[2, 4, 6]

BrokenBenchmark
- 18,126
- 7
- 21
- 33