+ 5
What does it mean when we write list[::-1]? What is the output of it??
list index
4 Réponses
+ 12
The proper syntax is:
[start:end:step] and each of the parameters may be omitted. Negative numbers mean counting backwards.
So:
[start::] will return the list starting from 'start' until the last element
[:end:] will return all from the first element until the one preceding 'end'
[0:-3] will return elements starting from the first one until (but not including) the third to last
[::2] will return every second element of the list
[::-3] will return every third element counting backwards
+ 3
list[::-1] will return the list backwards
+ 3
Suppose for example :
list= [1,2,3,4]
print (list [::-1])
We get the output as[ 4,3,2,1]
By this we can understand that using ::-1 reverses the list
+ 2
i tried to print list[::-2] but the out but defferint :/