About a[:-6:-1] where a = range(10)
2 Answer(s)
Deepak Kumar
Aug 12 2016 12:36 PM
Hi Cao,
The syntax of list slicing is
seq = my_list [start: stop: step]
So in this case, when you are trying with a = [0,1,2,3,4,5,6,7,8,9] and slicing as a[:-6:-1] it reads the elements in reverse direction as step is -1 and cretes the list from starting index to the index -6(excluding index -6).So the output(new list created) is
>>> a[:-6:-1]
[9, 8, 7, 6, 5]
Similarly, in [:-6:-2], the step is -2 and hence the interpreter will read each alternative element from the last index.So the result is
>>> a[:-6:-2]
[9, 7, 5]
Shaik
Aug 21 2016 10:34 AM
Hi Cao,
Interesting question, as we discussed in the session
column name[first index: last index:sorting]
when we want to sort in descending order we use the (second : ).
Hope it helps