List getting extended when assigned to itself
Krishnan Shankar
i.am.songoku at gmail.com
Sat Aug 24 23:52:27 EDT 2013
Hi Python Friends,
I came across an example which is as below,
>>> var = [1, 12, 123, 1234]
>>> var
[1, 12, 123, 1234]
>>> var[:0]
[]
>>> var[:0] = var
>>> var
[1, 12, 123, 1234, 1, 12, 123, 1234]
>>>
Here in var[:0] = var we are assigning an entire list to the beginning of
itself. So shouldn't it be something like,
[[1, 12, 123, 1234], 1, 12, 123, 1234]
It happens when we do the below,
>>> var = [1, 12, 123, 1234]
>>> var[0] = var
>>> var
[[...], 12, 123, 1234]
>>>
Literally var[0] = var and var[:0] = var almost meens the same. But why is
the difference in output? Can anyone explain what happens when
slicing assignment and direct assignment.
Regards,
Krishnan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130825/22b6415e/attachment.html>
More information about the Python-list
mailing list