This syntax is a slice assignment. A slice of [:]
means the entire list. The difference between nums[:] =
and nums =
is that the latter doesn't replace elements in the original list. This is observable when there are two references to the list
>>> original = [1, 2, 3]
>>> other = original
>>> original[:] = [0, 0] # changes the contents of the list that both
# original and other refer to
>>> other # see below, now you can see the change through other
[0, 0]
To see the difference just remove the [:]
from the assignment above.
>>> original = [1, 2, 3]
>>> other = original
>>> original = [0, 0] # original now refers to a different list than other
>>> other # other remains the same
[1, 2, 3]
To take the title of your question literally, if list
is a variable name and not the builtin, it will replace the length of the sequence with an ellipsis
>>> list = [1,2,3,4]
>>> list[:] = [...]
>>> list
[Ellipsis]