Why does the below code returns an empty list when it should return [4, 3, 2]
(because of the negative step size):
i = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
s = i[2:-5:-1]
print(s)
It starts counting from "2" going backwards (step -1) until "-5" is reached - but reaching the element "-5" would require positive step in this case.
For example the output of:
i[2::-1]
is:
[2, 1, 0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With