0
[::-1]: what is mean by this in python
2 Answers
+ 5
Reverse the list or string:
a={1,2}
print(a[::-1])
Displays:
[2, 1]
+ 4
The square bracket is the slicing operator.
It can have 2 or 3 parameters and each can be omitted also, and they can be negative.
iterable[start : end : step]
start is the first index position where the slice begins
end is the last index that is no longer included in the slice
step means to jump elements while slicing, if it is negative then the slice goes backwards, towards the beginning of the list.
That's why [::-1] means "reverse", it takes all elements starting from the last one, going backwards.