+2 votes
in Programming Languages by (74.2k points)
How can I reverse a Python list in Pythonic style?

1 Answer

+1 vote
by (350k points)
selected by
 
Best answer

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]

Related questions

+2 votes
1 answer
+1 vote
1 answer

...