-1

So I know how to get the sum of a single list so say,

a=[1,2,3,4]

sum(a)

10

How would I go about trying to sum lists in a list of lists? So, from:

[[1,2],[2,3],[3,42]] to

[3,5,45]?

3 Answers3

2

Code:

l = [[1,2],[2,3],[3,42]]
print([sum(i) for i in l])

Output:

[3, 5, 45]
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
1

You should use sum() in list comprehension for other lists

In [12]: a = [[1,2],[2,3],[3,42]]                                                                                                                                                                                                                             

In [13]: [sum(i) for i in a]                                                                                                                                                                                                                                  
Out[13]: [3, 5, 45]
frankegoesdown
  • 1,898
  • 1
  • 20
  • 38
-1
intial_list = [[1,2],[2,3],[3,42]]
res = list(map(sum, intial_list))
print(res)

output

 [3,5,45]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44