34

In Java, I have the following for loop and I am learning Python:

for (int index = last-1; index >= posn; index--)

My question is simple and probably obvious for most of people who are familiar with Python. I would like to code that 'for' loop in Python. How can I do this?

I tried to do the following:

for index in range(last-1, posn, -1):

I think it should be range(last-1, posn + 1, -1). Am I right?

I am thankful to anyone especially who will explain to me how to understand the indices work in Python.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Jika
  • 1,556
  • 4
  • 16
  • 27
  • It says `IndexError: list index out of range` – Jika Mar 27 '15 at 02:14
  • as an aside: depending on what your doing you may prefer enumerate: `for i, e in reversed(list(enumerate(list_here))):` where i would count from last element and e would be the actual elements – LinkBerest Mar 27 '15 at 02:14
  • 1
    @Jika So look at the indices generated by range. Then fix your code. – user2864740 Mar 27 '15 at 17:25

4 Answers4

61

The range function in python has the syntax:

range(start, end, step)

It has the same syntax as python lists where the start is inclusive but the end is exclusive.

So if you want to count from 5 to 1, you would use range(5,0,-1) and if you wanted to count from last to posn you would use range(last, posn - 1, -1).

Loocid
  • 6,112
  • 1
  • 24
  • 42
  • And just to be sure, this -1 is because it's stepping backwards from 10 to 0? – timman Apr 22 '20 at 22:42
  • 1
    @timman Correct. If you wanted to step backwards from 10 to 5 for instance, it would be `range(10, 4, -1)`. – Loocid Apr 23 '20 at 00:24
19

In python, when you have an iterable, usually you iterate without an index:

letters = 'abcdef' # or a list, tupple or other iterable
for l in letters:
    print(l)

If you need to traverse the iterable in reverse order, you would do:

for l in letters[::-1]:
    print(l)

When for any reason you need the index, you can use enumerate:

for i, l in enumerate(letters, start=1): #start is 0 by default
    print(i,l)

You can enumerate in reverse order too...

for i, l in enumerate(letters[::-1])
    print(i,l)

ON ANOTHER NOTE...

Usually when we traverse an iterable we do it to apply the same procedure or function to each element. In these cases, it is better to use map:

If we need to capitilize each letter:

map(str.upper, letters)

Or get the Unicode code of each letter:

map(ord, letters)
chapelo
  • 2,519
  • 13
  • 19
8

First I recommand you can try use print and observe the action:

for i in range(0, 5, 1):
    print i

the result:

0
1
2
3
4

You can understand the function principle. In fact, range scan range is from 0 to 5-1. It equals 0 <= i < 5

When you really understand for-loop in python, I think its time we get back to business. Let's focus your problem.

You want to use a DECREMENT for-loop in python. I suggest a for-loop tutorial for example.

for i in range(5, 0, -1):
    print i

the result:

5
4
3
2
1

Thus it can be seen, it equals 5 >= i > 0

You want to implement your java code in python:

for (int index = last-1; index >= posn; index--)

It should code this:

for i in range(last-1, posn-1, -1)
Burger King
  • 2,945
  • 3
  • 20
  • 45
-4

If you google. "Count down for loop python" you get these, which are pretty accurate.

how to loop down in python list (countdown)
Loop backwards using indices in Python?

I recommend doing minor searches before posting. Also "Learn Python The Hard Way" is a good place to start.

Community
  • 1
  • 1