1

Ok guys, Why does this simple for-loop fail to fill a list? Here is what I have:

axx = []
for i in range(1,7):
  axx.append('ax'+str(i))
  print(axx[i])

I am getting an error:

Traceback (most recent call last):
  File "./bin_data2.py", line 57, in <module>
    print(axx[i])
IndexError: list index out of range

This should pretty simple and straightforward, isn't it? Sorry, coming from Fortran background!

user3578925
  • 881
  • 3
  • 16
  • 26

3 Answers3

5

You need i-1 while printing. Array is appended starting from 0 and i starts from 1. So, at first append, there is element at 0 but i has value of 1:

print(axx[i-1])

If you look into result from:

print(list(range(1,7)))

Output:

[1, 2, 3, 4, 5, 6]

Or, you may change the range to range(6) instead of range(1,7). However, it will print with value of 0. In that case, you want axx.append('ax'+str(i+1))

niraj
  • 17,498
  • 4
  • 33
  • 48
  • Still doesn't work. It shouldn't matter as I am not accessing the `axx[0]`. Something is wrong with the for-loop and list assignment. – user3578925 Sep 16 '18 at 01:58
  • 1
    For above code, it did give result fine. When you are calling `print` first time then i has value of 1, but the list only has element at 0 so, it is out of index. – niraj Sep 16 '18 at 02:00
  • Alternatively, stop bothering with `i` when printing; `axx[-1]` will always show the final item in the `list`, and since you append on the previous line, it's always the value you just appended. – ShadowRanger Sep 16 '18 at 02:03
  • So in this case, `for i in range(1,7): axx.append('ax'+str(i))` is somewhat redundant, as python will always start the array index from 0. So, as far as I can understand `for i in range(1,7)` should be replaced by `for i in range(1,7)` . – user3578925 Sep 16 '18 at 02:06
  • I think what you meant is replace `for i in range(1,7)` with `for i in range(6)` then, it will start from 0 till 5 i.e. less than last element. And in your `append`, you may want as `str(i+1)`. – niraj Sep 16 '18 at 02:09
  • 1
    Thanks for the help! Never mind. It seems you can't have a list `[ax1,ax2,ax3..]` without the index of `ax1` being 0. I guess that's how python works. You will have `[ax1,ax2,ax3..]` but have to keep in mind that ax1 is actually the zero-th element, which is a bit confusing when you try to access it. – user3578925 Sep 16 '18 at 02:14
  • As far as I know, that's how indexing of array is on other languages such as `C`, `C++`, `Java` as well. Arrays in `R` has starting from `1`, I think – niraj Sep 16 '18 at 02:16
  • 1
    @student There are [a few others too](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array)#Array_dimensions): the better-known ones include Lua, MATLAB/Octave/Julia, and Smalltalk. The vast majority of languages have zero-indexed arrays, though. – Jack Taylor Sep 16 '18 at 02:23
  • @JackTaylor Thanks! I did not know much about other languages :) – niraj Sep 16 '18 at 02:26
3

Lists are indexed from zero. Your loop loops over [1, 2, 3, 4, 5, 6], or six times. Your list axx will end up a list with length six, indexes zero to five ([0, 1, 2, 3, 4. 5]). But you access it as i, which is one greater than the current index. So you will need to subtract one from it to get the value at the right position.

So don't use range(1,7), use range(6).

Keith
  • 42,110
  • 11
  • 57
  • 76
0

Don't forget python uses zero-based indexing. Your range (1,7) is going from 1 to 6, but your list has an item at place 0 after the first loop, so your print statement is always one i behind. Change range function to be range(7), or change print function to be print(i).

axx = []
for i in range(7):
    axx.append('ax'+str(i))
    print(axx[i])

output: ax0 ax1 ax2 ax3 ax4 ax5 ax6

OR

axx = []
for i in range(1,7):
    axx.append('ax'+str(i))
    print(i)

output: 1 2 3 4 5 6

If you don't want your list to include ax0, try:

axx = []
for i in range(7):
    axx.append('ax'+str(i+1))
    print(axx[i])

output: ax1 ax2 ax3 ax4 ax5 ax6 ax7

Also consider next time printing the whole list after it was been populated, so you know what your code is doing. This is the same code you had originally, just without printing i each time you append.

axx = []
for i in range(1,7):
    axx.append('ax'+str(i))

print(axx)

output: ['ax1', 'ax2', 'ax3', 'ax4', 'ax5', 'ax6']

Kristin
  • 113
  • 1
  • 7