There are a couple of ways to reverse a list in python:
|
|
Of these three ways, I prefer using reversed
because it is readable and there’s no ambiguity about whether it does it in place or not. It should be obvious to most people that .reverse()
reverses a list in place,
but I prefer the verbosity of the method that returns a reversed list. It’s a stylistic choice.
The [::-1]
syntax though is unreadable unless you know where it comes from. The syntax is List[start:stop_at:step_count]
. This can be used to collect items of a list in a specific way. You could use it to collect all the alternate items in a list through my_list[::2]
, or every third item from the first to the seventh index, through my_list[0:7:2]
(If you don’t want to include the last item, remember that python’s list intervals are half-open).
So [::-1]
means collect all the items in this array but move through it -1 items at a time, meaning, start with the last item first and move backwards.
But which one of these is faster? We can use the timeit module to test this out.
|
|
This attempts to reverse a list of a thousand numbers ranging from 0 to a million ten thousand times. On my X13 Flow laptop and with python 3.12.3 this gets me the following output:
0.0005502169997271267
0.0013474229999701492
0.012336962999597745
I’m fairly surprised myself that reversed
is the fastest way to reverse a list in Python. I half expected to tell you that it’d be the list notation syntax. And it’s faster than the in-place method, which isn’t surprising since it’s not trying to move things around to save memory. This StackOverflow answer shows that it’s creating an iterator and not returning a list like the list notation syntax would, so using list(reversed(my_list))
would be a fairer comparison.
|
|
Now we get a more realistic solution.
trial_1= 0.0005320420000316517
trial_2= 0.0013524949999919045
trial_3= 0.011964209999860032
trial_4= 0.032413619000180915
Honestly though, this doesn’t prove anything. If I needed a reversed list to iterate over, the iterator approach with reversed
is the best way to do it. If I need an in-memory version of it, the list notation syntax is clean. In-place reversal is useful only if I’m completely sure I would like to reverse the list in place. This is useful when the list is perhaps an attribute of a class, for example.