Iterate through List with range

Question:

I’m confused with some python basics.
I have my list:

myList = ['a','b','c','d','e','f']

now I would like to print this list using index and range:

for i in range(0, len(myList)):
    print(myList[i])

I got:

a
b
c
d
e
f

My first question: Why I shouldn’t use len(myList)-1? len(myList) returns 6 but when I use directly print(myList[6]) I got out of range error. Why using this in for loop is different?

Second. I know I can use myList.reverse() but I would like to print reversed list like this:

for i in range(len(myList), 0, -1):
    print(myList[i])

I get out of range and I should add -1:

for i in range(len(myList)-1, 0, -1):
    print(myList[i])

but after this I got only:

f
e
d
c
b

My second question: where is “a”? 😉 and why in this example I have to use len(myList)-1?

Asked By: Mlody87

||

Answers:

The first prameter is inclusive, whereas the second parameter is exclusive. Which means if a range function is called like range(a,b) it would result in values between [a,b). Therefore when you use range(0, len(myList)), this loops through 0..len(myList)-1.

As you would have guessed by now, you need to subtract 1 while looping in reverse order because the last index of the array is len(myList) -1 and you are missing a because 0 is not included. Therefore, if you wish to iterate in reverse order, you could do range(len(myList)-1, -1, -1).

Answered By: Swetank Poddar

First of all know that indexes start from 0 and not 1. When you go for myList[6], first of all, it doesn’t exist because index start from 0. If you have 6 elements, the the last index is 5. So, myList[5] is ‘f’.
Now, range() works such that when you give arguments as range(0, len(myList)), it iterates from 0 to len(myList)-1. It iterates upto 1 less than the given argument.

For your second question, again you will have to start with len(myList)-1 because last index is 5 in your case. And about where is a, you will have to go till range(len(myList)-1,-1,-1) so that it goes up till 0th index.

Answered By: dewDevil

First question, why: python works that way, odd at firs but normal when you become used to it. Range[a, b] gives: a, a+1, a+2, … b-1 (includes the first parameter and excludes the last).

It means that for the second question you should use:

for i in range(len(myList)-1, -1, -1):
    print(myList[i])
Answered By: jlbmdm

Several things to note:

  1. Python is zero-indexed
    This explains the IndexError when executing myList[6] because the last element is #5 not #6.
  2. range(start, stop) is “front-inclusive” and “end-exclusive”
    So while iterating, you print myList[0] but not myList[6], which is why you did not get an IndexError here.
  3. range(n)
    n represents the number of times to execute the for loop, so because you would like to print 6 elements, you should do range(len(myList)) without -1.
  4. range(start, stop, step)
    Again, Python’s for loops are “front-inclusive” and “end-exclusive” so you should start with the index of the last element (len(myList)-1) and end with the index of the first element minus 1 (-1). Step size can be thought of as the “displacement” while iterating, so with a step size of -1, you decrease the iterator i by one at each epoch.

Hope this helped!

Answered By: ccl
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.