Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one reverse the order of a list in Python without using a loop?

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

like image 300
Info5ek Avatar asked Dec 04 '22 10:12

Info5ek


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

like image 76
tailor_raj Avatar answered Dec 07 '22 00:12

tailor_raj