Early on in my exploration of Python, I remember being told that using pop
on the first element of a list wasn't necessarily bad practice, but was less than optimal. The reason that was given was that basically a new list had to be formed to be able to re-index all of the remaining elements. This doesn't appear to be the case, however:
test_list = [1, 2, 3, 4, 5]
hex(id(test_list))
Out[29]: '0x17d5b172f48'
test_list.pop(0)
Out[30]: 1
hex(id(test_list))
Out[31]: '0x17d5b172f48'
Nonetheless, I'm still wordering if there is some overhead to reassigning indices to the remaining elements, or if there is some other cost to popping from any other element but the last.
EDIT
Honestly, the difference between popping the last and first element does not look all that trivial when working with large lists:
test_list = list(range(int(1e6)))
%timeit test_list.pop(0)
671 µs ± 26 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
test_list = list(range(int(1e5)))
%timeit test_list.pop(0)
The slowest run took 5.01 times longer than the fastest. This could mean that an intermediate result is being cached.
17.3 µs ± 7.91 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
test_list = list(range(int(1e8)))
%timeit test_list.pop()
91.7 ns ± 0.821 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)