Select as dictionary...

Stargaming stargaming at gmail.com
Mon Oct 1 10:05:42 EDT 2007


On Mon, 01 Oct 2007 09:57:46 -0400, J. Clifford Dyer wrote:

> On Mon, Oct 01, 2007 at 06:32:07AM -0700, Besturk.Net Admin wrote
> regarding Select as dictionary...:
>> 
>> aia.execute("SELECT id, w from list") links=aia.fetchall()
>> print links
>> 
>> and result
>> [(1, 5), (2,5).......] (2 million result)
>> 
>> I want to see this result directly as a dictionary:
>> 
>> {1: 5, 2: 5 .....}
>> 
>> How do i select in this format ?
>> 
> Try this:
> 
> aia.execute("SELECT id, w from list") 
> links=aia.fetchall()
> linkdict = dict()
> for k,v in links:
>     linkdict[k] = v
> print linkdict

Besides using the already pointed out DB adapters, you can easily 
transform a list of items into a dictionary with the `dict` constructor::

    >>> links = [(1, 5), (2, 5), (3, 10)]
    >>> dict(links)
    {1: 5, 2: 5, 3: 10}

Stargaming



More information about the Python-list mailing list