0

what is [::-1] in python

explain about the above declaration in python

10th Jul 2019, 7:27 AM
Yaswanth D
Yaswanth D - avatar
2 Answers
+ 1
It is a slice operator, which somehow reverses any ordered iterables.
10th Jul 2019, 7:53 AM
Seb TheS
Seb TheS - avatar
0
First try to understand this code: x=[0,1,2,3,4,5] #using ":" will print everything print( x[ : ] ) >>>[0,1,2,3,4,5] #if we want to step 2 numbers we can use third argument as print( x[ 0 : 5 : 2 ] ) >>>[0,2,4] #if we want to start from back we write in the same way but with "-" sign (note: from back the index of 1st digit is -1 not 0 print( x[ -1 : -5 : -2] ) >>>[5,3,1] #now if we want to to print everything without giving indices of 1st two arguments we can use ":" as described earlier(here we used two colons to skip middle argument and include the 3rd argument) print( x[ : : -2] ) >>>[4,2,0] #and if we don't want to skip we can write as print( x[ : : -1] ) >>>[5,4,3,2,1,0] I hope now you have understood the reversing of list.
7th Apr 2020, 3:08 AM
Tricker