First of all, list.append
and dict.__setitem__
are both O(1) average case. Of course they will have different coefficients, but there is not really any blanket reason to say that one or the other will be the faster. The coefficients may change depending on implementation detail, too.
Secondly, a more fair comparison would be to remove the attribute resolution overhead:
>>> timeit.timeit('test[0] = ("test")', setup='test = {}')
0.0813908576965332
>>> timeit.timeit('test_append("test")', setup='test = []; test_append = test.append')
0.06907820701599121
The lookup of the method name on the instance is relatively expensive, when you are looking at an extremely cheap operation such as append
.
I also see lists being consistently a little faster, once there is some data inside. This example is python 3.5.2:
>>> dict_setup = 'import random; test = {random.random(): None for _ in range(1000)}'
>>> list_setup = 'import random; test = [random.random() for _ in range(1000)]; test_append=test.append'
>>> timeit.timeit('test[0] = "test"', setup=dict_setup)
0.06155529400166415
>>> timeit.timeit('test_append("test")', setup=list_setup)
0.057089386998995906