What does colon at assignment for list[:] = [...] do in Python
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]
Note: vincent thorpe's comments below are either incorrect or irrelevant to the question. This is not a matter of value vs reference semantics.
nums = foo
rebinds the name nums
to refer to the same object that foo
refers to.
nums[:] = foo
invokes slice assignment on the object that nums
refers to, thus making the contents of the original object a copy of the contents of foo
.
Try this:
>>> a = [1,2]
>>> b = [3,4,5]
>>> c = a
>>> c = b
>>> print(a)
[1, 2]
>>> c = a
>>> c[:] = b
>>> print(a)
[3, 4, 5]