Which list operation is faster?
list.pop(0) or del list[0]?
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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With