Why do I get List index out of range when trying to add consecutive numbers in a list using for i in list

0 votes

Given the following list

a = [0, 1, 2, 3]

I want to create a new list b, which consists of elements for which the current and next values of a are summed. It will contain 1 less element than a.

Like this:

b = [1, 3, 5]

Here's what I've tried:

b = []

for i in a:

    b.append(a[i + 1] + a[i])

The trouble is I keep getting this error:

IndexError: list index out of range

Can somone help me with this?

Apr 24, 2022 in Python by Kichu
• 19,040 points
128 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

+1 vote
1 answer
0 votes
0 answers
0 votes
1 answer

Python: Sort list of lists numerically

You can try and De-dupe it with ...READ MORE

answered May 20, 2019 in Python by SDeb
• 13,300 points
1,660 views
0 votes
0 answers

How to return dictionary keys as a list in Python?

Now, in Python >= 3.3, I get something ...READ MORE

Feb 18, 2022 in Python by Dev
• 6,000 points
80 views
0 votes
0 answers

Double the value of array on a match - Python

I want to iterate through the elements ...READ MORE

Apr 24, 2022 in Python by Kichu
• 19,040 points
856 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,100 points
1,198 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP