python - How do I reverse a list or loop over it backwards? - Stack Overflow
Why does this "[::-1]" return a reversed list in Python? - Stack Overflow
Beginner question: assigning variable to list.reverse()
Reversing a list in Python?
Videos
Why would a double colon reverse a list? Is this something we just have to accept or is there some logic?
a = ['corge', 'quux', 'qux', 'baz', 'bar', 'foo'] print(a[::-1])
To get a new reversed list, apply the reversed function and collect the items into a list:
>>> xs = [0, 10, 20, 40]
>>> list(reversed(xs))
[40, 20, 10, 0]
To iterate backwards through a list:
>>> xs = [0, 10, 20, 40]
>>> for x in reversed(xs):
... print(x)
40
20
10
0
>>> xs = [0, 10, 20, 40]
>>> xs[::-1]
[40, 20, 10, 0]
Extended slice syntax is explained here. See also, documentation.
Hey, I'm learning lists in Python. When I try to use the reverse method to reverse my list it returns 'None'. I've read online that apparently this is because it doesn't actually change the list but I'm not sure what that means tbh. Even if it was a temporary modification, wouldn't it print that temporarily modified version of the list instead of printing 'None'? I found another solution (assuming the list is stored in my_list variable), print(my_list[::-1]). I understand that the -1 is referring to the end of the list (and maybe telling it to count back from there), but I have no idea what the '::' means. Would appreciate some help, thanks.
newlist = oldlist[::-1]
The [::-1] slicing (which my wife Anna likes to call "the Martian smiley";-) means: slice the whole sequence, with a step of -1, i.e., in reverse. It works for all sequences.
Note that this (and the alternatives you mentioned) is equivalent to a "shallow copy", i.e.: if the items are mutable and you call mutators on them, the mutations in the items held in the original list are also in the items in the reversed list, and vice versa. If you need to avoid that, a copy.deepcopy (while always a potentially costly operation), followed in this case by a .reverse, is the only good option.
Now let's timeit. Hint: Alex's [::-1] is fastest :)
$ p -m timeit "ol = [1, 2, 3]; nl = list(reversed(ol))"
100000 loops, best of 3: 2.34 usec per loop
$ p -m timeit "ol = [1, 2, 3]; nl = list(ol); nl.reverse();"
1000000 loops, best of 3: 0.686 usec per loop
$ p -m timeit "ol = [1, 2, 3]; nl = ol[::-1];"
1000000 loops, best of 3: 0.569 usec per loop
$ p -m timeit "ol = [1, 2, 3]; nl = [i for i in reversed(ol)];"
1000000 loops, best of 3: 1.48 usec per loop
$ p -m timeit "ol = [1, 2, 3]*1000; nl = list(reversed(ol))"
10000 loops, best of 3: 44.7 usec per loop
$ p -m timeit "ol = [1, 2, 3]*1000; nl = list(ol); nl.reverse();"
10000 loops, best of 3: 27.2 usec per loop
$ p -m timeit "ol = [1, 2, 3]*1000; nl = ol[::-1];"
10000 loops, best of 3: 24.3 usec per loop
$ p -m timeit "ol = [1, 2, 3]*1000; nl = [i for i in reversed(ol)];"
10000 loops, best of 3: 155 usec per loop
Update: Added list comp method suggested by inspectorG4dget. I'll let the results speak for themselves.