List copying idiom was Re: [Python-Dev] implementation of copy standard lib

Tom Anderson twic at
Wed Aug 17 02:00:48 CEST 2005

On Tue, 16 Aug 2005, Ron Adam wrote:

> Simon Brunning wrote:
>> On 8/14/05, Martijn Brouwer <e.a.m.brouwer at> wrote:
>>> I can imagine that *a lot* of libs/scripts use the copy library,
>> I think that copy is very rarely used. I don't think I've ever imported it.
> I use copy.deepcopy() sometimes, and more often [:] with lists. 
> Dictionary objects have a copy method.  All non mutable objects are 
> copies.

When you say [:], do you mean that you copy lists like this:

l = someList()
m = []
m[:] = l


That's what i've been doing. The other day, i realised that i could just 

l = someList()
m = list(l)

The same works for copying dicts. Whilst it's still not utterly obvious 
that what this is about is making a copy, it's more obvious, cleaner, 
shorter and probably faster than the slice assignment, which is really 
kludgy (almost intercalorific, in fact - or even perlish!).

> I too have wondered why copy isn't a builtin,

Same here. It seems like a sort of obvious thing to have, and could 
probably implemented much more simply and quickly in the interpreter. 
You'd probably want a __copy__ hook for classes which want special 
handling, and just do a normal deep copy for everything else.


Orange paint menace

More information about the Python-list mailing list