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
  • 31,737
  • 7
  • 30
  • 50
AJD98
  • 93
  • 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

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
  • 31,737
  • 7
  • 30
  • 50
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,433
  • 2
  • 21
  • 49
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
  • 131,796
  • 23
  • 122
  • 195
1

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

Agilanbu
  • 2,615
  • 2
  • 27
  • 31
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
  • 237
  • 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
  • 874
  • 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
  • 147,904
  • 31
  • 244
  • 302