Reversing a List in Python

Sometimes we need to reverse the order of the elements in a Python list. While there can be many different ways of implementing this task, I found three in particular that I appreciate for different reasons. Let’s define first a list of integers that we will reverse later.

1
l = [1, 2, 3, 4, 5, 6]

List slicing

This method can be a bit obscure at first read, but basically it slices the whole list proceding in the reverse order:

1
2
[input]: print l[::-1]
[output]: [6, 5, 4, 3, 2, 1]

Reversed method

We use the reversed method that returns an iterable object and a list comprehension to generate the new list

1
2
[input]: print [x for x in reversed(l)]
[output]: [6, 5, 4, 3, 2, 1]

### Swapping values in place

This last method is more verbose and it basically divides the list in two parts and swaps the first with sixth, the second with fifth, etc…

1
2
3
4
5
6
[input]:
for i in range(0, len(l) / 2):
    l[i], l[len(l) -1 -i] = l[len(l) -1 -i], l[i]
print l

[output]: [6, 5, 4, 3, 2, 1]
comments powered by Disqus
source code available on GitHub
Built with Hugo
Theme Stack designed by Jimmy