is list always with reference ?
Christopher Myers
chris.myers at ingenta.com
Wed Aug 28 09:33:37 EDT 2002
As all other posts have said, the simple answer to your question is
"Yes."
But, I assume you probably would like to get around this behavior.
The best way I know of is to use splicing.
>>> list1=[1,2,3,4,5,6]
>>> list2=list1
>>> list1
[1, 2, 3, 4, 5, 6]
>>> list2
[1, 2, 3, 4, 5, 6]
>>> list1.append(7)
>>> list1
[1, 2, 3, 4, 5, 6, 7]
>>> list2
[1, 2, 3, 4, 5, 6, 7]
>>> #reassign list2 using splicing
...
>>> list2 = list1[:]
>>> list1
[1, 2, 3, 4, 5, 6, 7]
>>> list2
[1, 2, 3, 4, 5, 6, 7]
>>> list1.append(8)
>>> list1
[1, 2, 3, 4, 5, 6, 7, 8]
>>> list2
[1, 2, 3, 4, 5, 6, 7]
>>>
Hans Nowak wrote:
>
> quite black wrote:
> > Hi,all~
> >
> > when we build up a list(for example named list1) and write this line:
> >
> > list2=list1
> >
> > So, list2 is reference with list1, alright ?
>
> Sort of. list1 and list2 are names for the same object. This is true for all
> Python objects, by the way, not just lists.
>
> > What I want to know is if
> > all modifications on list1 will affect list2 ? I say "All" here.
>
> Yes, since they are the same object.
>
> HTH,
>
> --
> Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
> # decode for email address ;-)
> The Pythonic Quarter:: http://www.awaretek.com/nowak/
> Kaa:: http://www.awaretek.com/nowak/kaa.html
--
Christopher Myers, Graduate Software Developer
Ingenta, Inc.
12 Bassett St.
Providence, RI 02903
ph: 401.331.2014 x 102
em: chris.myers at ingenta.com
aim: chrismyers001
More information about the Python-list
mailing list