A=[1,2,3,4,5,6] print(A[2:5:-1]) output is [ ] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

A=[1,2,3,4,5,6] print(A[2:5:-1]) output is [ ]

why is output empty list and what does -1 Do their

8th Sep 2018, 6:17 PM
mahima rao
mahima rao - avatar
1 Answer
+ 8
The output will be []. The syntax is [start:stop:step]. A negative step makes it count backwards, so start has to be bigger than stop in this case. With a[2:5:-1], you're telling python to count upwards (from index 2 to index 4) in steps of -1. That won't work. a[5:2:-1] would work and the output would be [6, 5, 4]. Note that the end index is never included.
8th Sep 2018, 6:49 PM
Anna
Anna - avatar