Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster? `list.pop(0)` vs `del list[0]`?

Which list operation is faster? list.pop(0) or del list[0]?

like image 642
Ryan Y Avatar asked Dec 19 '25 03:12

Ryan Y


1 Answers

Disclaimer Quick and dirty benchmark:

Using IPython on Python 3.7.6, it appears that del list[0] is faster as it takes only about 65% of the time spent by list.pop(0).

Commands used:

## Baseline to be subtracted
%timeit lst = list(range(10))
# >> 230 ns ± 1.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

## list.pop(0) time taken
%timeit lst = list(range(10)); lst.pop(0)
# >> 281 ns ± 0.926 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

## del list[0] time taken
%timeit lst = list(range(10)); del lst[0]
# >> 263 ns ± 1.11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

(263-230)/(281-230) = 33/51 = 65%

like image 110
Ryan Y Avatar answered Dec 21 '25 19:12

Ryan Y