num_list_1 = [1,2,3,4]
sum of num_list_1 = 10
num_list_2 = [5,6,7,8]
sum of num_list_2 = 26
how would I be able to sum together num_list_1 and num_list_2.
I've tried doing it myself however as it is a list it wont let me concatenate them.
num_list_1 = [1,2,3,4]
sum of num_list_1 = 10
num_list_2 = [5,6,7,8]
sum of num_list_2 = 26
how would I be able to sum together num_list_1 and num_list_2.
I've tried doing it myself however as it is a list it wont let me concatenate them.
Get the sum of each list individually, and then sum the both scalar values to get the total_sum:
In [1733]: num_list_1 = [1,2,3,4]
In [1734]: num_list_2 = [5,6,7,8]
In [1737]: sum(num_list_1) + sum(num_list_2)
Out[1737]: 36
You could sum the concatenation of the two lists:
sum(num_list_1+num_list_2)
This is what I get using the python console:
>>>num_list_1 = [1,2,3,4]
>>>num_list_2 = [5,6,7,8]
>>>sum(num_list_1+num_list_2)
>>>36
Or you could just sum the sums:
sum(num_list_1) + sum(num_list_2)
which leads to the same output but in a probably faster way:
>>>num_list_1 = [1,2,3,4]
>>>num_list_2 = [5,6,7,8]
>>>sum(num_list_1) + sum(num_list_2)
>>>36
if you have several lists (more than 2) you could sum the sums by applying map to the results:
sum(map(sum,(num_list_1,num_list_2)))
+ acts as concatenating in case of lists, so
sum(num_list_1 + num_list_2) will help
num_list_1 = [1,2,3,4]
num_list_2 = [5,6,7,8]
print(sum(num_list_1) + sum (num_list_2))
print(sum(num_list_1+ num_list_2))
You can use:
>>> num_list_1 = [1,2,3,4]
>>> num_list_2 = [5,6,7,8]
>>> sum(num_list_1+num_list_2)
>>> 36
sum takes an iterable, so you can use itertools.chain to chain your lists and feed the resulting iterable to sum:
from itertools import chain
num_list_1 = [1,2,3,4]
num_list_2 = [5,6,7,8]
res = sum(chain(num_list_1, num_list_2)) # 36