List assignment with [:]
When you do
lst = anything
You're pointing the name lst
at an object. It doesn't change the old object lst
used to point to in any way, though if nothing else pointed to that object its reference count will drop to zero and it will get deleted.
When you do
lst[:] = whatever
You're iterating over whatever
, creating an intermediate tuple, and assigning each item of the tuple to an index in the already existing lst
object. That means if multiple names point to the same object, you will see the change reflected when you reference any of the names, just as if you use append
or extend
or any of the other in-place operations.
An example of the difference:
>>> lst = range(1, 4)
>>> id(lst)
74339392
>>> lst = [1, 2, 3]
>>> id(lst) # different; you pointed lst at a new object
73087936
>>> lst[:] = range(1, 4)
>>> id(lst) # the same, you iterated over the list returned by range
73087936
>>> lst = xrange(1, 4)
>>> lst
xrange(1, 4) # not a list, an xrange object
>>> id(lst) # and different
73955976
>>> lst = [1, 2, 3]
>>> id(lst) # again different
73105320
>>> lst[:] = xrange(1, 4) # this gets read temporarily into a tuple
>>> id(lst) # the same, because you iterated over the xrange
73105320
>>> lst # and still a list
[1, 2, 3]
When it comes to speed, slice assignment is slower. See Python Slice Assignment Memory Usage for more information about its memory usage.
The first one redefines the built-in name list
to point to some list.
The second fails with TypeError: 'type' object does not support item assignment
.
list[:]
will only work if there is already an object named list
that allows slice assignment.
Also, you shouldn't name variables list
because there is a built-in named list
which is the list
type itself.
list[:]
specifies a range within the list, in this case it defines the complete range of the list, i.e. the whole list and changes them. list=range(100)
, on the other hand, kind of wipes out the original contents of list
and sets the new contents.
But try the following:
a=[1,2,3,4]
a[0:2]=[5,6]
a # prints [5,6,3,4]
You see, we changed the first two elements with the assignment. This means, using this notation, you can change several elements in the list once.