List append vs add

Fredrik Lundh fredrik at pythonware.com
Fri May 25 10:04:04 EDT 2001


Thomas Wouters wrote:
> > > list.append('fred')
> > >
> > > compared with,
> > >
> > > list+=['fred']
>
> > There is no difference in result.  The former strikes me as clearer,
> > and might well be quicker.
>
> It is quicker, but not by much. The += version skips the method lookup,
> method call, and argument parsing that the .append version has to go
> through, at the cost of a function-lookup in the type slot.

on the other hand, the += solution has to create a temporary
one-element list for each append.

on my machine, the append solution is slightly faster under 2.0,
and more than 10% faster under 2.1.  ymmv, as usual.

Cheers /F





More information about the Python-list mailing list