[Tutor] Copying a list?
Bob Gailer
bgailer at alum.rpi.edu
Wed Aug 27 16:53:31 EDT 2003
At 02:25 PM 8/27/2003 -0700, Zak Arntson wrote:
>I feel pretty silly asking this, but is there a way to quickly copy a
>list? I'd love to be able to type:
>
>###
> >>> a = [1,2,3]
> >>> b = a
>###
>
>But that's a pass-by-reference. The quickest thing that comes to mind is:
>
>###
> >>> a = [1,2,3]
> >>> b = [i for i in a]
>###
>
>Which is okay, but not as obvious as, say:
>
>###
> >>> a = [1,2,3]
> >>> b= a.__copy__()
>###
>
>or something. Am I forgetting something?
Can't answer that, as we don't know what you formerly remembered.
To the original question:
b = a[:]
OR b = list(a)
Note that these are "shallow" copies; any object reference in the list will
be copied as is. See the copy module for deepcopy and more comments on copying.
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003
More information about the Tutor
mailing list