Why are dictionaries faster than lists in Python?
<pre><code>&gt;&gt;&gt; timeit.timeit('test.append("test")', setup='test = []')
0.09363977164165221
&gt;&gt;&gt; timeit.timeit('test[0] = ("test")', setup='test = {}')
0.04957961010914147
</code></pre>

<p>I even tried again with a loop, and same thing:</p>

<pre><code>&gt;&gt;&gt; timeit.timeit('for i in range(10): test.append(i)', setup='test = []')
1.3737744340367612
&gt;&gt;&gt; timeit.timeit('for i in range(10): test[i] = i', setup='test = {}')
0.8633718070233272
</code></pre>

<p>Why is the list slower?</p>

<p>First of all, <code>list.append</code> and <code>dict.__setitem__</code> 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.</p>

<p>Secondly, a more fair comparison would be to remove the attribute resolution overhead:</p>

<pre><code>&gt;&gt;&gt; timeit.timeit('test[0] = ("test")', setup='test = {}')
0.0813908576965332
&gt;&gt;&gt; timeit.timeit('test_append("test")', setup='test = []; test_append = test.append')
0.06907820701599121
</code></pre>

<p>The lookup of the method name on the instance is relatively expensive, when you are looking at an extremely cheap operation such as <code>append</code>. </p>

<p>I also see lists being consistently a little faster, once there is some data inside.  This example is python 3.5.2:</p>

<pre><code>&gt;&gt;&gt; dict_setup = 'import random; test = {random.random(): None for _ in range(1000)}'
&gt;&gt;&gt; list_setup = 'import random; test = [random.random() for _ in range(1000)]; test_append=test.append'
&gt;&gt;&gt; timeit.timeit('test[0] = "test"', setup=dict_setup)
0.06155529400166415
&gt;&gt;&gt; timeit.timeit('test_append("test")', setup=list_setup)
0.057089386998995906
</code></pre>

python dictionary