Converstion

Edward Elliott nobody at 127.0.0.1
Fri Apr 28 12:22:51 EDT 2006


Peter Otten wrote:
> 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:

But his data is a string, which is immutable but heavily optimized:

$ python -m timeit -n10000 -s'data = range(10000)' 'data = data[:-1]'
10000 loops, best of 3: 41.9 usec per loop

$python -m timeit -n10000 -s'data = range(10000)' 'del data[-1:]'
10000 loops, best of 3: 0.244 usec per loop

$ python -m timeit -n10000 -s'data = "abcdefghij"*1000' 'data = data[:-1]'
10000 loops, best of 3: 1.7 usec per loop

$ python -m timeit -n10000 -s'data = "abcdefghijklm"*1000' 'del data[-1:]'
[traceback omitted]
TypeError: object doesn't support slice deletion




More information about the Python-list mailing list