How can one reverse the order of a list in Python without using a loop? There are no other constraints on the solution space.
-
1`l.reverse()`. It's the *first Google result* for "Python reverse list", *right in the documentation*. – user2357112 Jul 19 '13 at 06:47
6 Answers
a = ['a','b','c']
you can try
b = a[::-1]
It will reverse the list without using loop.
You can use the same trick on any sequence/container/iterable to get item reversed.

- 1,037
- 2
- 9
- 19
-
-
1@user1800340 It's Python's [Slice Notation](http://stackoverflow.com/questions/509211/the-python-slice-notation) :) – TerryA Jul 19 '13 at 06:29
-
1I think I figured it out. 'list[::x]' means step in direction (sign of x) and magnitude x. Hence -1 would mean step backwards with each step being size 1. – Info5ek Jul 19 '13 at 06:34
-
1@user1800340 as mentioned by Haidro, it is slice notation of list. you can refer [official docs for list](http://docs.python.org/2/library/functions.html#list). It will explain everything. – tailor_raj Jul 19 '13 at 06:36
-
2There are three arguments in slice notation, start, stop and step, if u dont provide by default start will be 0, stop will be -1 means including everything in list, and step is -1 hence start from last element and iterate in reverse. – tailor_raj Jul 19 '13 at 06:39
-
2@user1800340 See [this link](http://bergbom.blogspot.in/2011/04/python-slice-notation.html) for more explanation. – tailor_raj Jul 19 '13 at 06:50
list.reverse()
Whether or not the underlying implementation uses a loop I don't know.

- 4,254
- 15
- 28
This reverse function is inefficient because of the way Python handles lists. However, it's worth understanding because it shows how to use a recursive function to replace a loop in a way that can be adapted to almost any language. (In other words, it doesn't depend on built-in features of Python.) Note that l[:1]
returns a single-item list containing the first item of l
and l[1:]
returns all the remaining items in l
.
>>> def reverse(l):
... return reverse(l[1:]) + l[:1] if l else l
...
>>> reverse([1, 2, 3, 4, 5])
[5, 4, 3, 2, 1]

- 145,869
- 36
- 209
- 233
You can also use the builtin function reversed
a = list(reversed(your_list))

- 45,752
- 12
- 65
- 86
-
1@septi Actually, `reversed` returns an iterator, so you need to use `list` to store a list in `a` instead of an iterator. – SethMMorton Jul 19 '13 at 06:33
Assuming you want the change in-place:
>>> a = [1,2,3]
>>> a.reverse()
>>> a
[3, 2, 1]
>>> a[:] = a[::-1]
>>> a
[1, 2, 3]

- 342,061
- 65
- 592
- 494
There are many ways:
You can use extended List Slicing:
>>> print [1, 2, 3, 4, 5][::-1]
[5, 4, 3, 2, 1]
Or use the built in .reverse()
function:
>>> L = [1, 2, 3, 4, 5]
>>> L.reverse()
>>> print L
[5, 4, 3, 2, 1]
Or use reversed()
, which returns the reversed list - but as an iterator:
>>> L = [1, 2, 3, 4, 5]
>>> print reversed(L)
<listreverseiterator object at 0x2c3630>
>>> print list(reversed(L))
[5, 4, 3, 2, 1]
Timing comparisons:
$ python -m timeit "L = range(1000)" "L = L[::-1]"
100000 loops, best of 3: 11.8 usec per loop
$ python -m timeit "L = range(1000)" "L.reverse()"
100000 loops, best of 3: 9.13 usec per loop
$ python -m timeit "L = range(1000)" "L = list(reversed(L))"
100000 loops, best of 3: 19.1 usec per loop