+ 5

What does it mean when we write list[::-1]? What is the output of it??

list index

14th Mar 2017, 1:19 PM
Aya Habeeb
Aya Habeeb - avatar
4 Respuestas
+ 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
14th Mar 2017, 4:44 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
list[::-1] will return the list backwards
14th Mar 2017, 1:42 PM
Marcus Søndergaard
Marcus Søndergaard - avatar
+ 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
19th Mar 2017, 2:40 AM
Syed Touhid
Syed Touhid - avatar
+ 2
i tried to print list[::-2] but the out but defferint :/
14th Mar 2017, 1:43 PM
Aya Habeeb
Aya Habeeb - avatar