which is more pythonic/faster append or +=[]

Alex Martelli aleax at mac.com
Wed May 9 01:05:12 EDT 2007


alf <ask at me> wrote:

> two ways of achieving the same effect
> 
> 
> l+=[n]
> 
> or
> 
> l.append(n)
> 
> 
> so which is more pythonic/faster?

.append - easy to measure, too:

brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loop

brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loop


Alex



More information about the Python-list mailing list