copy list, which way is best? /style

Andrew Wilkinson ajw126 at york.ac.uk
Wed Jul 9 08:28:38 EDT 2003


Andreas Kuntzagk wrote:

> Hi,
> 
> There are three ways to (shallow)copy a list l I'm aware of:
> 
>>>> l2=list(l)
>>>> l2=l[:]
>>>> l2.copy.copy(l)
> 
> Are there any differences? Are there more (reasonable) ways?
> I think the first is the most pythonic, second looks more like this other
> p-language and third requires an import, so I would prefer the first.
> Do you agree?
> 
> Andreas

The way I'd do it is 

from copy import copy
l2 = copy(l1)

or

from copy import deepcopy
l2 = deepcopy(l1)

I don't know what the difference is, if any, but I think this way is more
readable.

HTH,
Andrew Wilkinson

-- 
Study + Hard Work + Loud Profanity = Good Code






More information about the Python-list mailing list