Select as dictionary...

Tim Chase python.list at
Mon Oct 1 16:12:57 CEST 2007

> 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 .....}

Because your fortuitously issued a select in that particular 
order (key, value), you can simply use:

  my_dict = dict(aia.fetchall())

Alternatively, if you don't want to consume double-ish the 
memory, you can do something like

   my_dict = {}
   while True:
     rows = aia.fetchmany()
     if not rows: break

This should at least prevent both a 2-million-item list returned 
by fetchall() *and* the 2-million-item dict; leaving you instead 
with a footprint of a 2-million-item-dict, plus however many rows 
your fetchmany() brings back by default (or you can specify it as 
a optional parameter to fetchmany--see your help for fetchmany).

2 million results are a lot, no matter how big each element 
is...beware of memory limits.


More information about the Python-list mailing list