Which is faster a dictionary retrieve or a list indexing?
<p>I'm trying to decide which data structure best fits my needs.</p>

<p>Technical details apart, I may translate the needs of my program to use dictionaries or to use lists, and since performance will be an issue I was wondering what may be a faster solution. I ended up concluding that retrieving/indexing will be the critical operation.</p>

<p>So what's more efficient in terms of memory usage and speed?</p>

<p>Well, retrieving from a dict has the same complexity as retrieving from a list:</p>

<pre><code>_dict[1]
_list[1]
</code></pre>

<p>but..., with dicts, u can use:</p>

<pre><code>_dict.get(1, 'something else')
</code></pre>

<p>It just returns 'something else' if there is no key <code>1</code>. Using lists, you cannot do that. You just want to get item indexed by number <code>1</code>. If you will ask for item outside your list, so index will be higher than your list length, then your program will raise <code>IndexError</code> which must be handled. Next problem will be that you will have to know size of that list, so you need to check it first.</p>

python list dictionary profiling