Dictionary of tuples from query question

Fredrik Lundh fredrik at pythonware.com
Mon Nov 14 12:58:42 CET 2005


David Pratt wrote:

> My code so far. I guess my problem is how to generate a tuple
> dynamically when it is immutable?

you can use tuple() to convert lists to tuples.

    >>> x = []
    >>> x.append(1)
    >>> x.append(2)
    >>> x.append(3)
    >>> tuple(x)
    (1, 2, 3)

but doesn't fetchall already returns tuples, btw?  isn't something
like

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

an easier way to get the dictionary you want?

fwiw, if all you need is something that returns the right thing if you
do d[index], you could even do:

    d = [None] + list(cursor.fetchall())

(or you could wrap the fetchall() result in a trivial wrapper class that
adjusts the index on the fly)

</F>






More information about the Python-list mailing list