Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52962

List getting extended when assigned to itself

Date 2013-08-25 09:22 +0530
Subject List getting extended when assigned to itself
From Krishnan Shankar <i.am.songoku@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.206.1377402755.19984.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

List getting extended when assigned to itself Krishnan Shankar <i.am.songoku@gmail.com> - 2013-08-25 09:22 +0530
  Re: List getting extended when assigned to itself Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-25 05:31 +0000

csiph-web