How can I create a dictionary from a list of k:v tuples returned from a SQL query?
<p>My SQL (sqlite3) query is along the lines of this:</p>

<pre><code>SELECT id, name FROM mytable;
</code></pre>

<p>This returns a ton of rows.  I would like to create a dictionary from these results, where I can lookup the name given an id.</p>

<p>Other answers I've found seem to create dictionaries based on SQL results, but they use the column label as the key -- I have no interest in that.</p>

<p>Example table:</p>

<pre><code>id    | name
------+--------
foo   | bar
baz   | foobar
</code></pre>

<p>Desired dict:</p>

<pre><code>{
    'foo': 'bar',
    'baz': 'foobar'
}
</code></pre>

<p>The result returned from cursor is a list of 2-elements tuples so you can just convert that to a dictionary as the first item of each tuple is a unique value:</p>

<pre><code>result = dict(cursor.execute("SELECT id, name FROM mytable"))
</code></pre>

python sql dictionary sqlite3