Python List Issue
Terry Reedy
tjreedy at udel.edu
Sun Mar 27 12:39:38 EST 2005
"Nick L" <Fearnot003 at mchsi.com> wrote in message
news:ADu1e.23161$fn3.19315 at attbi_s01...
> I've hit a brick wall on something that I'm guessing is pretty simple but
> it's driving me nuts. I noticed that with python lists, generally when
> you
> make a copy of a list (ie, List1 = List2)
Python is not C, etc. Assigning a name to an object *never* makes a copy.
Assigning a second name creates an alias. Aliases are problems in any
language that allows them (which C does also, via pointers). Consider
Nick_PythonLearner = Nick_L
> List1 just becomes a reference to List2
Ditto for the two Nick names, but better said, both names refer to the same
person (or list).
> and any modifications done to List1 affects List2.
and ditto for a person with more than one name -- which is almost everyone
in the modern world (Nick, Mr. L, etc).
> Ok I can live with this
Good. Name confusion is everywhere.
> but I want to make a completely seperate [sic] copy not attached to the
> original in anyway. So then I used this method. List1 = List2[:]
For a shallow (one level) copy, List1=list(List2) is now prefered. For a
deep (complete, all-level) copy, follow Kern's advice: copy.deepcopy.
Terry J. Reedy
More information about the Python-list
mailing list