0

[::-1]: what is mean by this in python

24th Apr 2022, 2:58 AM
Muhammed Fazin.E. K
Muhammed Fazin.E. K - avatar
2 Answers
+ 5
Reverse the list or string: a={1,2} print(a[::-1]) Displays: [2, 1]
24th Apr 2022, 3:08 AM
John Wells
John Wells - avatar
+ 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.
24th Apr 2022, 4:42 AM
Tibor Santa
Tibor Santa - avatar