Adnan Akram
Adnan Akram

Reputation: 186

Sum of consecutive pairs in a list including a sum of the last element with the first

I have a list of elements like [1,3,5,6,8,7]. I want a list of sums of two consecutive elements of the list in a way that the last element is also added with the first element of the list. I mean in the above case, I want this list: [4,8,11,14,15,8]

But when it comes to the addition of the last and the first element during for loop, index out of range occurs. Consider the following code:

List1 = [1,3,5,6,8,7]
List2 = [List1[i] + List1[i+1] for i in range (len(List1))]

print(List2)

Upvotes: 7

Views: 480

Answers (9)

Vivek Shinde
Vivek Shinde

Reputation: 21

When i == 5, then it will enter the "if" statement and i is set to -1. Note that List[-1] is the same as List[len(List) - 1], which means that List[i+1] will become List[0], the first element of the list.

for i in range(len(List1)):
    if i == len(List1) - 1:
        i = -1
    List2.append(List1[i] + List1[i+1])

Upvotes: 1

assembly_wizard
assembly_wizard

Reputation: 2064

Some more pythonic answers that weren't answered yet:

Using map can be more readable:

List2 = list(map(sum, zip(List1, List1[1:] + List1[:1])))

You can also use itertools to offset the list:

import itertools
List2 = list(map(sum, zip(List1, itertools.islice(itertools.cycle(List1), 1, None))))

Upvotes: 1

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

[List1[i] + List1[(i+1) % len(List1)] for i in range(len(List1))]

or

[sum(tup) for tup in zip(List1, List1[1:] + [List1[0]])]

or

[x + y for x, y in zip(List1, List1[1:] + [List1[0]])]  

Upvotes: 4

RufusVS
RufusVS

Reputation: 4127

Here's another ugly solution:

List1 = [1,3,5,6,8,7] 
List2 = [List1[i] + (List1+[List1[0]])[i+1] for i in range (len(List1))]

print (List2)

Upvotes: 1

Vignesh Pillay
Vignesh Pillay

Reputation: 155

This rotates the list

In [9]: List1[1:] + List1[:1]                                                                 
Out[9]: [3, 5, 6, 8, 7, 1]

So following works perfectly

In [8]: [x + y for x, y in zip(List1, List1[1:] + List1[:1])]                                 
Out[8]: [4, 8, 11, 14, 15, 8]

Upvotes: 2

Asim
Asim

Reputation: 85

As you are running it to the len(List1) - 1 So when, i value will be len(List1) - 1, then i + 1 will be out of range (i.e. len(List1)), So you can try this solution:

List2 = [List1[i-1] + List1[i] for i in range (len(List1))]

Or you can do:

List2 = [List1[i] + List1[i+1] for i in range (len(List1)-1)]

Or you can use any other logic also. Cheers!

Upvotes: 1

M Z
M Z

Reputation: 4799

List2 = [List1[i] + List1[(i+1)%len(List1)] for i in range (len(List1))]

Upvotes: 4

NMrocks
NMrocks

Reputation: 108

What you're actually doing is adding consecutive items in the list, but when you reach the last index, your code still adds one to the last index, which results in an index out of range error. You should consider using:

List1 = [1,3,5,6,8,7]
List2 = [List1[i] + List1[i+1] for i in range (len(List1) - 1)]
List2.append(List1[0] + List1[-1])

print (List2)

Upvotes: 1

Hosein Shendabadi
Hosein Shendabadi

Reputation: 362

Because of the i+1, the index goes out of range

List1 = [1,3,5,6,8,7]
List2 = [List1[i-1] + List1[i] for i in range (len(List1))]

print (List2)

This way kinda works
Result:

[8, 4, 8, 11, 14, 15]

Upvotes: 2

Related Questions