[Tutor] Duplicating lists in variables?
Joseph J. Strout
joe@strout.net
Fri, 17 Sep 1999 12:10:31 -0700
>When I append bar, IOW, foo gets modified too. Is there a way for me to
>actually *duplicate* a list?
But of course! You could use the copy module, which can copy
anything. But there's a sneaky way to use the slice operator which
is more common:
bar = foo[:]
This works because the slice operator returns a new list which is a
subset of the original. But if the subset starts at the beginning,
and continues till the end, then it happens to be a copy of the
entire list!
Cheers,
-- Joe
,------------------------------------------------------------------.
| Joseph J. Strout Biocomputing -- The Salk Institute |
| joe@strout.net http://www.strout.net |
`------------------------------------------------------------------'