0
Confused with slicing output
Let's take a list a=[0,1,2,3]. When using positive step, the output is clear for me, e.g. print (a[1::2]) produces [1,3]. However, print (a[1::-2]) does not produce [3,1]. The output is [1]. How come?
1 Answer
+ 1
Same concept, starts from index 1, and goes back two steps. To print [3, 1], it should be: print(l[3::-2])
If you had a bigger list:
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[8::-3])
Output: [8, 5, 2]
It starts from index 8, goes back three steps, adds 5, go back three steps, adds 2, goes back three steps, out of range! Stops, returns 8, 5, 2