Dictionary of tuples from query question

Fredrik Lundh fredrik at pythonware.com
Mon Nov 14 17:12:18 CET 2005


David Pratt wrote:

> Hi Fredrik. Many thanks for your reply and for the tuple tip. The
> cursor.fetchall returns a list of lists in this instance with each list
> in the main list containing the field values. With the tip, I
> simplified my code to:
>
> vlist_dict = {}
> record_count = 0
> for record in cursor.fetchall():
> record_count += 1
> vlist_dict[record_count] = tuple(record)
> print vlist_dict
>
> That's much better!

I meant to write

    d = {}
    for index, record in enumerate(cursor.fetchall()):
        d[index+1] = tuple(record)

which can be shorted to

    d = dict((k+1, tuple(v)) for k, v in enumerate(x))

in recent versions of Python.

</F>






More information about the Python-list mailing list