Stroter
Stroter

Reputation: 27

How to sum every element in a list with the previous one?

I would like to receive the following result,

Example:

list1 = [145, 100, 125, 134, 556]

with the output being a new list with the sum results like this:

list2 = [145, 245, 225, 259, 690]

Upvotes: 1

Views: 1158

Answers (7)

Carmoreno
Carmoreno

Reputation: 1319

Python to the old school style!. I have created this function that takes as parameter a list and return a new list with the previous element summed. I hope can help you

def sum_list(list_numbers):
  index = 0
  list2 = [list_numbers[index]]
  while index < (len(list_numbers) - 1):
    item = list_numbers[index] + list_numbers[index + 1]
    list2.append(item)
    index += 1
  return list2

print(sum_list(list1))

Output:

[145, 245, 225, 259, 690]

Edit: I wanted to challenge myself using a recursion approach. Maybe it's not the best approach but is here:

list2 = []
def sum_list_recursion(first_element, list_numbers, max_length):

  if len(list_numbers) == 0:
    return list2

  elif len(list_numbers) == max_length:
    list2.append(first_element)
    next_element = list_numbers[0]
    list_numbers = list_numbers[1:]

  else:
    next_element = list_numbers[0]
    list_numbers = list_numbers[1:]
    list2.append((first_element + next_element))
  
  sum_list_recursion(next_element, list_numbers, max_length)

sum_list_recursion(list1[0], list1, len(list1))
print(list2)

Output:

[145, 245, 225, 259, 690]

Upvotes: 1

salt-die
salt-die

Reputation: 854

If you don't mind getting a little bit esoteric, this can be done in a single comprehension without copying the original list:

In [14]: list_1 = [145, 100, 125, 134, 556]
    ...: b = 0
    ...: list_2 = [b + (b := a) for a in list_1]
    ...: list_2
Out[14]: [145, 245, 225, 259, 690]

Upvotes: 4

Matthias Fripp
Matthias Fripp

Reputation: 18625

This is similar to Selcuk's answer but may be a little simpler:

list2 = [a + b for a, b in zip(list1, [0]+list1)]

Or if you don't want to use zip;

list2 = [
    list1[i] + (list1[i-1] if i > 0 else 0)
    for i in range(len(list1))
]

Upvotes: 3

Selcuk
Selcuk

Reputation: 59174

You can also use the zip trick:

>>> list(map(sum, zip(list1, [0]+list1)))
[145, 245, 225, 259, 690]

Upvotes: 4

RubberDuckProducer
RubberDuckProducer

Reputation: 247

I didn't come up with a list comprehension idea.

But if you wanna brute force, it'll be:

list1 = [145, 100, 125, 134, 556]
list2 = [list1[0]]
for i in range(1, len(list1)):
    list2.append(list1[i] + list1[i-1])

print(list2)

And you can get

[145, 245, 225, 259, 690]

Upvotes: 1

Andy Pavlov
Andy Pavlov

Reputation: 316

My two cents

list1 = [145,100,125,134,556]
list2 = [val if not idx else val + list1[idx - 1] for idx, val in enumerate(list1)]

Upvotes: 1

Buddy Bob
Buddy Bob

Reputation: 5889

I suggest using list comprehension for this.

list1 = [145,100,125,134,556]
newLst = [list1[0]]+[sum([list1[i],list1[i-1]]) for i in range(1,len(list1))]

output

[145, 245, 225, 259, 690]

Upvotes: 1

Related Questions