Converting tuples to dictionaries?

Mart van de Wege mvdwege.usenet at drebbelstraat20.dyndns.org
Thu Sep 20 16:58:30 EDT 2001


Hi Folks,

Apologies if this turns out to be a FAQ, but I couldn't find it in the standard
docs and the books I own on how to do it. So without further ado, I'll state
the question:

I have some data in an SQL database. Now AFAICT psycopg (aka pythonDB 2.0 for
PostgreSQL) returns the query results as a list of tuples. The data that
returns from my query would fit very well in a dictionary, ie. tuple[0] would
be the key, and the rest would make the value (or list of values), in fact the
conceptual model I have for my program would *need* a dictionary to process.

Now the obvious way to turn a list of tuples into a dictionary would be to loop
over the list and convert them by assigning tuple[0] to be the key and the rest
of the tuple the value. Slightly more efficient would be to define a function
that does that with one tuple and then map the function on the list.

The question is, is this really the only way to go about that, or am I barking
up the wrong tree entirely? It really seems like a brute force approach to me.
Is there an alternative?

TIA,

Mart

-- 
Requiem aeternam dona eis, Domine, et lux perpetua luceat eis.
Requiescant in pace.
Amen.



More information about the Python-list mailing list