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 in Python by Kichu
• 17,230 points
15 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
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
19,229 views
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,488 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 in Python by Dev
• 6,000 points
18 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 in Python by Kichu
• 17,230 points
25 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,060 points
1,038 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP