+ 1

Please explain this code

Hello. Please explain the second line: list = [0,1,2,3,4,5] print(list[:1:-1][1])

20th May 2019, 4:12 PM
Zakhar
2 Respuestas
+ 2
list[:1] would mean the list up to (but not including) index 1. However, list[:1:-1] means the list *going backwards* up to (but not including) index 1. So, for the list in your example, list[:1:-1] would be [5,4,3,2], since list[1] is 1 and so it is not included in the list slice. So then list[:1:-1][1] is [5,4,3,2][1] is 4.
20th May 2019, 4:18 PM
Russ
Russ - avatar
0
thanks 👍
20th May 2019, 4:22 PM
Zakhar