4

How can one reverse the order of a list in Python without using a loop? There are no other constraints on the solution space.

Henrik
  • 4,254
  • 15
  • 28
Info5ek
  • 1,227
  • 4
  • 17
  • 25

6 Answers6

7
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.

tailor_raj
  • 1,037
  • 2
  • 9
  • 19
  • Thanks. Can you explain how to interpret the bracketed arguments? – Info5ek Jul 19 '13 at 06:26
  • 1
    @user1800340 It's Python's [Slice Notation](http://stackoverflow.com/questions/509211/the-python-slice-notation) :) – TerryA Jul 19 '13 at 06:29
  • 1
    I 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
  • 2
    There 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
4

list.reverse()

Whether or not the underlying implementation uses a loop I don't know.

Datastructures - Python Documentation

Henrik
  • 4,254
  • 15
  • 28
2

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]
senderle
  • 145,869
  • 36
  • 209
  • 233
1

You can also use the builtin function reversed

a = list(reversed(your_list))
SethMMorton
  • 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
1

Assuming you want the change in-place:

>>> a = [1,2,3]
>>> a.reverse()
>>> a
[3, 2, 1]
>>> a[:] = a[::-1]
>>> a
[1, 2, 3]
DSM
  • 342,061
  • 65
  • 592
  • 494
1

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
Community
  • 1
  • 1
TerryA
  • 58,805
  • 11
  • 114
  • 143