Python loop: list index out of range

Given the following list:

a=[0,1,2,3] 

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

Like this: [1,3,5]

(from 0 + 1, 1 + 2 and 2 + 3)

Here is what I tried:

 b=[] for i in a: b.append(a[i+1]-a[i]) b 

The problem is that I keep getting this error:

 IndexError: list index out of range 

I am sure this is happening, because by the time I get the last element (3), I can’t add it to anything, because it goes beyond its value (after the value there is no 3 value to add). So I need to say that the code stops at 2, but still refers to 3 for calculation.

Thanks in advance!

+6
source share
4 answers
  • In your for loop, you repeat the elements of list a . But in the body of the loop, you use these elements to index this list when you really need indexes.
    Imagine if list a contains 5 elements, there will be 100 among them, and the for loop will reach it. You will essentially try to extract the 100th element of list a , which obviously is not. This will give you an IndexError .

We can fix this problem by iterating over a range of indices:

 for i in range(len(a)) 

and access such elements a : a[i] . This will not give any errors.

  1. In the body of the loop, you index not only a[i] , but also a[i+1] . This is also the place for potential error. If your list contains 5 elements, and you repeat it, as I showed in paragraph 1, you will get an IndexError . What for? Since range(5) is essentially 0 1 2 3 4 , so when the loop reaches 4, you try to get the element a[5] . Since indexing in Python starts at 0 and your list contains 5 elements, the last element has an index of 4, so getting a[5] means getting the sixth element that doesn't exist.

To fix this, you must subtract 1 from len(a) to get a sequence of ranges 0 1 2 3 . Since you are using the i+1 index, you will still get the last element, but this way you will avoid the error.

  1. There are many ways to do what you are trying to do here. Some of them are quite elegant and more "pythonic", for example, in the form of lists:

b = [a[i] + a[i+1] for i in range(len(a) - 1)]

This task runs on only one line.

+8
source

When you call for i in a: you get the actual elements, not the indexes. When we reach the last element, that is 3 , b.append(a[i+1]-a[i]) searches for a[4] , does not find it, and then fails. Instead, try iterating over the indices, stopping only for the last one, for example

for i in range(0, len(a)-1): Do something

Your current code will not work if you do something;)

+1
source

Try decreasing the range of the for loop to range(len(a)-1) :

 a = [0,1,2,3] b = [] for i in range(len(a)-1): b.append(a[i]+a[i+1]) print(b) 

This can also be written as a list comprehension :

 b = [a[i] + a[i+1] for i in range(len(a)-1)] print(b) 
+1
source

You access the list items and then use them to try to index your list. This is not a good idea. You already have an answer showing how you can use indexing to get your list of amounts, but another option would be a zip list with a snippet that you can sum pairs.

 b = [i + j for i, j in zip(a, a[1:])] 
+1
source

All Articles