How does Python's slice notation work? That is: when I write code like a[x:y:z]
, a[:]
, a[::2]
etc., how can I understand which elements end up in the slice?
See Why are slice and range upper-bound exclusive? to learn why xs[0:2] == [xs[0], xs[1]]
, not [..., xs[2]]
.
See Make a new list containing every Nth item in the original list for xs[::N]
.
See How does assignment work with list slices? to learn what xs[0:2] = ["a", "b"]
does.