The list can be reversed in several ways.
As mentioned in previous answers, two are very outstanding, one with a reverse() function and two with a slice function. I talk about which one we would prefer. We should always use the reverse() function to access a Python list. Two reasons: one U-turn in place and two faster than the others. I have numbers to support my answer,
In [15]: len(l) Out[15]: 1000 In [16]: %timeit -n1 l.reverse() 1 loops, best of 3: 7.87 ยตs per loop In [17]: %timeit -n1 l[::-1] 1 loops, best of 3: 10 ยตs per loop
For 1000 whole lists, the reverse() function performs better than slicing.
Aashish P Dec 29 '15 at 9:44 2015-12-29 09:44
source share