0

I need to add every index of an unknown amount of lists together into one list.

An example of what I mean:

list_a = [1,2,3,4,5]
list_b = [2,4,6,8,10]
list_c = [3,6,9,12,15]

def sum_lists():
    temp_list = []

    for index in range(len(list_a)):
        temp_list.append(list_a[index] + list_b[index] + list_c[index])

    return temp_list

total = sum_lists()

The expected output of my example code would be:

total = [6,12,18,24,30]

How would I accomplish the summation of an unknown amount of lists, for example 20 lists? I won't have to do this addition over thousands of lists, but I wouldn't know how many lists I have to add initially.

Any help would be greatly appreciated. All of the lists will be of the same length.

cs95
  • 379,657
  • 97
  • 704
  • 746
Code Noob
  • 3
  • 3
  • If you had to get the sum of a single list (e.g. `[1, 2, 3] → 6`), how would you do it? – Ry- Aug 12 '17 at 01:01
  • 2
    `zip` the lists. In the resulting list of tuples, sum the elements of each tuple. Look up how to use the `zip` and `sum` methods. – Prune Aug 12 '17 at 01:02
  • 1
    `[sum(els) for els in zip(list_a, list_b, list_c)]`. As @Prune said, this uses the [`sum`](https://docs.python.org/3/library/functions.html#sum) and [`zip`](https://docs.python.org/3/library/functions.html#zip) builtin methods. – Christian Dean Aug 12 '17 at 01:03
  • Not exact dupe but close: https://stackoverflow.com/questions/19261747/sum-of-n-lists-element-wise-python. – Christian Dean Aug 12 '17 at 01:05
  • If you are using a for loop, the answer you accepted isn't the most efficient. It would be better to zip the lists together, iterate over the elements directly and sum those. You should reconsider what answer you want to use. – cs95 Aug 12 '17 at 01:31
  • @coldspeed You are correct. I started with Elliot's answer, then used yours and the comments to arrive at the function. I verified the one I ended up using, not the one that helped the most which was a mistake on my my part. Thanks for all of your help – Code Noob Aug 12 '17 at 17:14

3 Answers3

0

You can store lists in lists and sum over them.

You could have something like this:

list_a = [1,2,3,4,5]
list_b = [2,4,6,8,10]
list_c = [3,6,9,12,15]

list_of_lists = [list_a, list_b, list_c] # this could be 20+ lists

def sum_lists():
    temp_list = []

    for index in range(len(list_a)):
        temp_list.append(sum([l[index] for l in list_of_lists]))

    return temp_list

total = sum_lists()
Elliot Godzich
  • 216
  • 2
  • 12
0

Create a list of lists:

In [124]: big_list = [list_a, list_b, list_c]

Now, zip em up and apply sum to each one using map:

In [125]: list(map(sum, zip(*big_list)))
Out[125]: [6, 12, 18, 24, 30]

You have other alternatives to map. For example, using a list comprehension:

In [126]: [sum(x) for x in zip(*big_list)]
Out[126]: [6, 12, 18, 24, 30]
cs95
  • 379,657
  • 97
  • 704
  • 746
-1

Here's my alternative:

list_a = [1, 2, 3, 4, 5]
list_b = [2, 4, 6, 8, 10]
list_c = [3, 6, 9, 12, 15]

def sum_lists(*arg):

    return [sum(i) for i in zip(*arg)]


results = sum_lists(list_a, list_b, list_c)
rug3y
  • 104
  • 4