Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About slices in Python

Tags:

python

slice

  1. Is there any difference between mylist[:] and mylist[::]?
  2. What's the rationale for mylist[::0] to raise an error since negative steps are allowed?
like image 583
ChessMaster Avatar asked Oct 24 '25 01:10

ChessMaster


2 Answers

  1. No. Both result in slice(None, None, None).

  2. Positive strides go forwards. Negative strides go backwards. Zero strides go... nowhere? How exactly would that work? An infinite sequence of a single value?

like image 145
Ignacio Vazquez-Abrams Avatar answered Oct 26 '25 15:10

Ignacio Vazquez-Abrams


No difference between mylist[:] and mylist[::]

mylist[::0]

This implies from starting index to last index without any step, don't know in what world it would be possible.

like image 20
avasal Avatar answered Oct 26 '25 16:10

avasal