Why is slice assignment faster than `list.insert`?
问题 Inspired by this nice answer, Here's a benchmark: import timeit def test1(): a = [1,2,3] a.insert(0,1) def test2(): a = [1,2,3] a[0:0]=[1] print (timeit.timeit('test1()','from __main__ import test1')) print (timeit.timeit('test2()','from __main__ import test2')) For me, test2 is sligtly faster (~10%). Why is that the case? I would expect it to be slower since: slice assignment must be able to accept iterables of any length and therefore must be more general. in slice assignment, we need to