[Python-ideas] Have list/deque grow a copy() method

Josiah Carlson jcarlson at uci.edu
Tue May 15 22:54:07 CEST 2007


Georg Brandl <g.brandl at gmx.net> wrote:
> Josiah Carlson schrieb:
> > "Ian D. Bollinger" <ian.bollinger at gmail.com> wrote:
> >> It would be nice if containers had a more consistent interface. The
> >> standard idiom for copying a list has been L[:], but this isn't
> > 
> > The standard way of copying a list (or producing a list from an
> > arbitrary sequence) is list(L).
> 
> The first part is news to me. Who defined that standard?

No one needs to define a standard for it to be the standard.  How would
you propose, generally, to convert some arbitrary sequence into a list?
Would you use a list comprehension: [i for i in seq] ?  Would you use a
generator expression passed to a list: list(i for i in seq) ?  Or would
you just use list(seq)?  Me, I prefer simplicity and the case that works
the way I usually need it to: list(seq).  Conveniently, this works for
list(dct), list(lst), list(deq), list(st), list(uni), list(tup),
list(ite), ...  To not use list(obj) when you can would seem to me to be
an anti-pattern, regardless of the minor opimization that can be had in
lst[:] copying.

This technique is also going to be used in the 2to3 conversion utility
to convert range() calls to list(range()) calls, and I am honestly
baffled by anyone asking, "how do I copy a list, deque, dictionary,...". 
How could one *not* look at the base constructors?

 - Josiah




More information about the Python-ideas mailing list