Converting tuples to dictionaries?

Simon Brunning SBrunning at trisystems.co.uk
Fri Sep 21 04:47:39 EDT 2001


> From:	Mart van de Wege [SMTP:mvdwege.usenet at drebbelstraat20.dyndns.org]
> 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?
 
Marc-André Lemburg's mxTools (<http://www.lemburg.com/files/python/>)
includes a dict function which does *almost* what you want...

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list