how can i copy a sublist

Chris Gonnerman chris.gonnerman at usa.net
Thu Mar 1 09:08:45 EST 2001


The key word is copy.  Python lists are copied with the [:] (all-slice)
notation, as in:

    b.append(a[0][:])

Hope this helps...

----- Original Message ----- 
From: "Daniel Orlowski" <daniel.orlowski at bg-sys.de>
Subject: how can i copy a sublist


> Hi,
> 
> 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
> [[1, 2]]
> >>> b[0][0]=3
> >>> a
> [[3, 2], [3, 4]]
> 
> how can i copy a sublist without
> "get_a_value_from_a_list_and_put_it_into_another"
> 
> Regards,
> Daniel
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list