+1 vote
in Programming Languages by (8.7k points)
How can I reverse a Python list in Pythonic style?

1 Answer

0 votes
by (17k points)

You can use one of the following approaches to reverse a list in a Pythonic way.

Approach 1

>>> a
[1, 2, 3, 4, 5]
>>> b=a[::-1]
>>> b
[5, 4, 3, 2, 1]

Approach 2

>>> a
[1, 2, 3, 4, 5]
>>> a.reverse()
>>> a
[5, 4, 3, 2, 1]

Approach 3

>>> a
[1, 2, 3, 4, 5]
>>> [i for i in reversed(a)]
[5, 4, 3, 2, 1]

...