how can i copy a sublist
David Porter
jcm at bigskytel.com
Thu Mar 1 09:02:32 EST 2001
* Daniel Orlowski <daniel.orlowski at bg-sys.de>:
> i try to copy a sublist from list "a" to list "b". After that i alter a
> value in "b" and oops! value in "a" is also altered.
> so it looks like that:
> >>> a=[[1,2],[3,4]]
> >>> b=[]
> >>> b.append(a[0])
b.append(a[0][:])
This copies the list items and creates a new object. OTOH, the original
references the same object that a[0] references. Therefore, when b[0] is
changed, so is a[0] (because it is really the object that they both
reference that is changed).
hth,
David
More information about the Python-list
mailing list