Best way to create a copy of a list

Alex Martelli aleaxit at yahoo.com
Tue Apr 4 11:41:47 EDT 2006


Frank Millman <frank at chagford.com> wrote:
   ...
> If they are all equivalent from a functional point of view, I lean
> towards the second version. I agree with Rune that the third one is
> nicer to read, but somehow the [:] syntax makes it a bit more obvious
> what is going on.

I vastly prefer to call list(xxx) in order to obtain a new list with the
same items as xxx -- couldn't be more obvious than that.

You can't claim it's obvious that xxx[:] *copies* data -- because in
Numeric, for example, it doesn't, it returns an array that *shares* data
with xxx.  So, the [:] notation sometimes copies and sometimes does not,
list list(...) always copies -- if I want to ensure that a copy does
happen, then list(...) is the more obvious and readable choice.


Alex



More information about the Python-list mailing list