reverse reverses the list in-place, see the manual, while [::-1] gives a new list in reversed order.
Try print(p) after calling p.reverse(), you'll see the difference.
reverse reverses the list in-place, see the manual, while [::-1] gives a new list in reversed order.
Try print(p) after calling p.reverse(), you'll see the difference.
Regarding reversing list in python, the 3 main ways:
the built-in function
reversed(seq)that will return a reverseiteratorwhich is an object representing the stream of data that will return successive items of this steam. Generating this reverse iterator isO(1)in time/space complexity and using it to iterate through the elements of a list will beO(N),O(1)in time/space complexity where N is the length of the list. This is the approach you want to go for if you simply want to iterate on the reversed list without modifying it. This approach gives the best performances in this case.p.reverse()reverses the list in-place. The operation isO(N),O(1)in time/space complexity as it will have to go through half of the elements of the list to reverse them and it doesn't store the results in a new list.p.reverse()will returnNoneand the elements will be directly reversed inpafter this instruction. This approach is good if you do NOT need to keep the original list and you have several passes/operations to do on the elements of the reversed list and if you have to save the processed reversed list.Using slicing
[::-1]creates a new object of the list/a copy in reversed order. The operation isO(N),O(N)in space/time complexity as you have to copy all the elements of the list in the new one and this new list will also consume the same amount of space as the original list. In terms of reference, you will have a new object created so even ifprint(p == x)returnsTrue,print(p is x)will returnFalseas the two objects even if they share the same values have different references. This approach is great if you need to keep the original list and have a reversed copy of it stored in a different object for further processing.
To summarize, depending on your use case you will have to use one of those 3 approaches that have slightly different objectives and completely different performances.
For example:
txt = "Hello World"[::-1]
Isn't the splice syntax [start : stop: step]? And default of start and stop are the beginning and end of the string? So that would make the above start at the beginning, stop at the end, but step by -1. That feels like it would start at the beginning, then step backwards to...before the beginning of the string?
Sorry for the silly question, I just can't figure out why this syntax works the way it does.
Videos
I always thought that slice operation for reversing of the list or something else is fastest one. But after watching "Transforming Code into Beautiful, Idiomatic Python" on youtube I have decided to compare slice and reversed method to each other. Obviously reversed is faster than slicing when we assign the result to new variable. But when we want just to reverse without having new variable results are roughly the same or even better for slicing . Here is the graph and tested code
graphs!
import timeit example = ''' d = [1,2,3,4,5,6,7,8,9,0] d = reversed(d) ''' example2 = ''' d = [1,2,3,4,5,6,7,8,9,0] d = d[::-1] ''' print(timeit.timeit(example, number=100000000)) print(timeit.timeit(example2, number=100000000)) import timeit example = ''' d = [1,2,3,4,5,6,7,8,9,0] new_var = reversed(d) ''' example2 = ''' d = [1,2,3,4,5,6,7,8,9,0] new_var = d[::-1] ''' print(timeit.timeit(example, number=100000000)) print(timeit.timeit(example2, number=100000000))
Should programmer choose carefully between slicing and reversed?
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])