how can i copy a sublist

Burkhard Kloss bk at xk7.com
Thu Mar 1 15:04:49 CET 2001


Look at copy.copy
>>> from copy import copy
>>> a=[[1,2],[3,4]]
>>> b = []
>>> b.append (copy (a [0]))
>>> b
[[1, 2]]
>>> b[0][0]=3
>>> b
[[3, 2]]
>>> a
[[1, 2], [3, 4]]

Also have a look at the documentation for copy.deepcopy to see which is more
appropriate in your case.

"Daniel Orlowski" <daniel.orlowski at bg-sys.de> wrote in message
news:3A9E4C6F.E2DD63C3 at bg-sys.de...
> 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
>
>





More information about the Python-list mailing list