I have a question about slicing the Python list.

Asked 2 years ago, Updated 2 years ago, 66 views

[start:end:step] when slicing the list.

a=[1,2,3,4,5]

a[:] #[1,2,3,4,5]
a[0:5] #[1,2,3,4,5]
with the same return value

a[::-1] #[5,4,3,2,1]
a[0:5:-1] #[]
For a[0:5:-1], the return value is changed by the empty list form.

If step is negative with certain values in start and end, I get a return value differently than expected, but I'm not sure how it's sliced.

python slice

2022-09-21 19:39

1 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.