3
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.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
AJD98
  • 103
  • 9
  • 2
    `sum(num_list_1 + num_list_2 )` should work ( the + is the concatenation but for list) – Frayal Dec 04 '18 at 10:08
  • Possible duplicate of [Sum a list of numbers in Python](https://stackoverflow.com/questions/4362586/sum-a-list-of-numbers-in-python) – Rahul Agarwal Dec 04 '18 at 10:11
  • This is not clear. **What should the result be**? Do we want to concatenate the lists, like `[1,2,3,4,5,6,7,8]`? Do we want the the sum of that list (`36`)? If we want `36`, are we concatenating the sublists first and then summing, or summing each and then adding the two subtotals, or does it matter? Or do we want to add values *element-wise*, like `[6,8,10,12]`? Or something else completely? – Karl Knechtel Sep 08 '22 at 08:32

7 Answers7

6

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
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

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
magicleon94
  • 4,887
  • 2
  • 24
  • 53
1

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)))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

+ acts as concatenating in case of lists, so sum(num_list_1 + num_list_2) will help

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
1

First Define both lists

num_list_1 = [1,2,3,4]
num_list_2 = [5,6,7,8]

then use Sum() For Both list

print(sum(num_list_1) + sum (num_list_2))

Also You Can Do This :

print(sum(num_list_1+ num_list_2))
Ankit Singh
  • 247
  • 4
  • 14
0

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
Tom
  • 918
  • 6
  • 19
Sunil Game
  • 29
  • 5
0

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
jpp
  • 159,742
  • 34
  • 281
  • 339