117

I could be wrong (just let me know and I'll delete the question) but it seems python won't respond to

for n in range(6,0):
    print n

I tried using xrange and it didn't work either. How can I implement that?

Gal
  • 23,122
  • 32
  • 97
  • 118
  • Out of curiosity, are you using this for anything? It's rather uncommon to need this functionality! – Katriel Nov 27 '10 at 22:07
  • @katrielalex I use it to iterate over a matrix from right to left. is there a different way you suggest I should do it? – Gal Nov 27 '10 at 23:33
  • you could try `for i in reversed(mat):` although that might be slower – Katriel Nov 27 '10 at 23:42
  • Please take a look at [Print a list in reverse order with range](https://stackoverflow.com/questions/7286365/print-a-list-in-reverse-order-with-range), It offers some other helpful information. – R.F Dec 12 '17 at 21:27
  • 1
    Please take a look [Print a list in reverse order with range](https://stackoverflow.com/questions/7286365/print-a-list-in-reverse-order-with-range), the accepted answer explains it very clearly. – R.F Dec 12 '17 at 21:31
  • @Katriel This is handy for creating a list of rectangles in Tkinter using `x0, y0, x1, y1` tuples where the top of rectangle (`y0`) is greater than the bottom (`y1`). I haven't coded it yet but I believe this question has merit for that purpose and up-voted it as a consequence. – WinEunuuchs2Unix Jan 31 '21 at 00:03

8 Answers8

271
for n in range(6,0,-1):
    print n
# prints [6, 5, 4, 3, 2, 1]
Steve Tjoa
  • 59,122
  • 18
  • 90
  • 101
  • 31
    Betcha OP actually wanted `range(5,-1,-1)`. Although he could probably figure that out from trial and error. – kojiro Aug 26 '13 at 01:20
57

This is very late, but I just wanted to add that there is a more elegant way: using reversed

for i in reversed(range(10)):
    print i

gives:

4
3
2
1
0
pratikm
  • 4,034
  • 7
  • 25
  • 23
  • 2
    What's elegant about that? You spend time reversing a list instead of generating it the way you want it. – alexis Mar 18 '12 at 00:01
  • 11
    @alexis it doesn't cost anything. You get `reversed(range)` for free because of the nice [`range_reverse`](https://github.com/python-git/python/blob/master/Objects/rangeobject.c#L151) optimization built into CPython. I did some quick benchmarks and couldn't find a significant cost difference between `step=-1` and `reversed()` in both Python 2.7 and 3.3. Also please note that [this idiom is used in heapq](http://hg.python.org/cpython/file/3.3/Lib/heapq.py#l179). – kojiro Aug 26 '13 at 01:34
  • Thanks, @kojiro, that's interesting. But unless you used `xrange` in your Python 2.7 tests, `reverse` will be operating on an ordinary, already-generated list, not on a range object; so are you saying _any_ list can be efficiently reversed, or just `range/xrange` objects? (the heapq code you link to involves a Python 3 range object). – alexis Aug 26 '13 at 21:05
  • 1
    @alexis I wouldn't be so bold as to suggest that any list can be efficiently reversed – that's too unqualified a statement for me to answer anyway. It's telling, though, that the heapify code changed from `step=-1` to `reversed()` between Python [2.3](http://hg.python.org/cpython/file/2.3/Lib/heapq.py#l168) and [2.4](http://hg.python.org/cpython/file/2.4/Lib/heapq.py#l175) – kojiro Aug 26 '13 at 21:33
  • 3
    `reversed(range(10))` can't possibly output `4` through `0`. Perhaps you meant `range(5)`? – Abhijit Sarkar Dec 29 '19 at 01:07
15
for n in range(6,0,-1)

This would give you 6,5,4,3,2,1

As for

for n in reversed(range(0,6))

would give you 5,4,3,2,1,0

Handy Jodana
  • 151
  • 1
  • 2
3
for n in range(6,0,-1):
    print n
cji
  • 6,635
  • 2
  • 20
  • 16
3
>>> range(6, 0, -1)
[6, 5, 4, 3, 2, 1]
vanza
  • 9,715
  • 2
  • 31
  • 34
2

0 is conditional value when this condition is true, loop will keep executing.10 is the initial value. 1 is the modifier where may be simple decrement.

for number in reversed(range(0,10,1)):
print number;
Neo
  • 31
  • 1
  • 4
1

Late to the party, but for anyone tasked with creating their own or wants to see how this would work, here's the function with an added bonus of rearranging the start-stop values based on the desired increment:

def RANGE(start, stop=None, increment=1):
    if stop is None:
        stop = start
        start = 1

    value_list = sorted([start, stop])

    if increment == 0:
        print('Error! Please enter nonzero increment value!')
    else:
        value_list = sorted([start, stop])
        if increment < 0:
            start = value_list[1]
            stop = value_list[0]
            while start >= stop:
                worker = start
                start += increment
                yield worker
        else:
            start = value_list[0]
            stop = value_list[1]
            while start < stop:
                worker = start
                start += increment
                yield worker

Negative increment:

for i in RANGE(1, 10, -1):
    print(i)

Or, with start-stop reversed:

for i in RANGE(10, 1, -1):
    print(i)

Output:

10
9
8
7
6
5
4
3
2
1

Regular increment:

for i in RANGE(1, 10):
    print(i)

Output:

1
2
3
4
5
6
7
8
9

Zero increment:

for i in RANGE(1, 10, 0):
    print(i)

Output:

'Error! Please enter nonzero increment value!'
Mark Moretto
  • 2,344
  • 2
  • 15
  • 21
0

For python3 where -1 indicate the value that to be decremented in each step for n in range(6,0,-1): print(n)

SREERAG R NANDAN
  • 593
  • 5
  • 15