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


Groups > comp.lang.python > #12924

Re: I am confused by

Date 2011-09-07 16:31 -0700
From Gary Herron <gherron@digipen.edu>
Subject Re: I am confused by
References <CAO_vtCZt1CTZnN9ss-X3RM5a=09PhkS0sHo00H4g3soeAQkKFw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.853.1315438788.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 09/07/2011 03:57 PM, Martin Rixham wrote:
> Hi all
> I would appreciate some help understanding something. Basically I am 
> confused by the following:
>
> >>> a = [[0, 0], [0, 0]]
> >>> b = list(a)
> >>> b[0][0] = 1
> >>> a
> [[1, 0], [0, 0]]
>
> I expected the last line to be
>
> [[0, 0], [0, 0]]
>
> I hope that's clear enough.
>
> Martin

You were expecting the assignment to "b" to create a copy of the 
original list, and it does, but the copy is only one level deep.

So a and b are different lists, but a[i] and b[i] are the same objects 
for each i.

There is a "deepcopy" in the library which may do what you expect.

Gary Herron


-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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


Thread

Re: I am confused by Gary Herron <gherron@digipen.edu> - 2011-09-07 16:31 -0700

csiph-web