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]?
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]?
Code:
l = [[1,2],[2,3],[3,42]]
print([sum(i) for i in l])
Output:
[3, 5, 45]
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]
intial_list = [[1,2],[2,3],[3,42]]
res = list(map(sum, intial_list))
print(res)
output
[3,5,45]