Best way to create a copy of a list

Frank Millman frank at chagford.com
Wed Apr 5 01:10:10 EDT 2006


Alex Martelli wrote:
> 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

Thanks very much for the detailed explanation.

Frank




More information about the Python-list mailing list