is list always with reference ?

Nikola Plejic Nikola.Plejic at pu.CARNet.hr
Tue Aug 27 12:57:57 CEST 2002


Actually, not all of them...

If you do this:

>>> list1 = ["my", "sample", "list"]
>>> list2 = list1
>>> list2
['my', 'sample', 'list']
>>> list2 = list1 * 2
>>> list2
['my', 'sample', 'list', 'my', 'sample', 'list']
>>> list1
['my', 'sample', 'list']

"holger krekel" <pyth at devel.trillke.net> wrote in message
news:mailman.1030436799.22986.python-list at python.org...
> 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 ? What I want to know is if
all modifications on list1 will affect list2 ? I say "All" here.
> >
>
> yes, they will. It often helps if you fire up the python commandline:
>
> [hpk at cobra pythonics]$ python
>
> Python 2.2.1 (#1, Aug 20 2002, 17:44:37)
> [GCC 2.95.3 20010315 (release)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> list1=[1,2,3]
> >>> list2=list1
> >>> list1.append(4)
> >>> list2
> [1, 2, 3, 4]
> >>>
>
> cheers,
>
>     holger
>





More information about the Python-list mailing list