Converstion

Peter Otten __peter__ at web.de
Fri Apr 28 11:39:57 EDT 2006


Paddy wrote:

> the del version - is that an optimisation?
> Is it actually faster?

del x[-1:] # or del x[-1] if you are sure that len(x) > 0

just deletes the last item (if any) from x whereas

x = x[:-1]

copies all but the last item of the original list into a new one. This can
take much longer:

[copy 10000 lists with 5000 items on average]
$ python -m timeit -n10000 -s'data = range(10000)' 'data = data[:-1]'
10000 loops, best of 3: 38.9 usec per loop

[remove the last item from a list 10000 times]
$ python -m timeit -n10000 -s'data = range(10000)' 'del data[-1:]'
10000 loops, best of 3: 0.272 usec per loop
$ python -m timeit -n10000 -s'data = range(10000)' 'del data[-1]'
10000 loops, best of 3: 0.246 usec per loop

Peter



More information about the Python-list mailing list