Why this code output is [] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this code output is []

lst=list(range(0,10)) Print(lst[-1:2])

27th May 2019, 6:54 PM
Geek
Geek - avatar
2 Answers
+ 4
lst[-1:2] is the same as lst[-1:2:1]. Go from the last element back to the third element in steps of +1. That's not possible, so the result is an empty list. If you change it to lst[-1:2:-1]), the result will be [9, 8, 7, 6, 5, 4, 3]
27th May 2019, 8:23 PM
Anna
Anna - avatar
0
Thankyou!!
1st Jun 2019, 6:40 AM
Geek
Geek - avatar