[PYTHON DB-SIG] DB-Modules returning lists of dicts

Christian Tismer tismer@tismer.com
Thu, 09 Jan 1997 16:23:53 +0100

Hey David,

> Hey folks,
> May I assume that due to the lack of response (with the exception
> of Glenn Colby's, thanks for your comments Glenn), that there is
> no interest out there in Python database modules giving the *option*
> to return a list of dictionaries as an alternative to the list
> of tuples that are currently returned?
[...deleted stuff...]
> I'm very interested in knowing why folks find this option undesirable.
> (if they actually do)

You are right that it is to some convenience if you have the result
rows of a query in dict form. But, as has been said here, the overhead 
is considerable.

Two cases:
1) Your database is small.
Then you can afford to do the dict view in Python, using something like
# names=("name1", "name2", "name3")
# row=("one","two","three")

def asDict(names, row) :
    res = {}
    for i in range(len(names)) : res[names[i]] = row[i]
    return res

2) Your database is large.
Then you definately will hunt for speed and optimize your row handling
to use few space, and setup index arrays to select columns of, say, a
large array of returned rows (tuples),
and the names are very seldom necessary and can be found easily.
Again, you would go with the above example which is Python standard
temporary code one writes over and over.

Moreover, there are a lot of other return structures possible and
necessary, depending of the application, and it would be impossible
to provide for every imagineable need. I could imagine that another
one wants the result in the form 
[("Name1", "Name2", "Name3"), ("Value1", "Value2", "Value3)]
and the next wants it split into key and nonkey name and so on...

For that reason, and the fact that the dict version looses natural 
order, I would suggest to leave it.

- chris

Christian Tismer - tismer@appliedbiometrics.com
Our support pages: <http://www.appliedbiometrics.com/python>
Got a real operating system? No? Try at least a real language: Python
<http://www.python.org> & <http://www.python.org/ftp/python/pythonwin>

DB-SIG  - SIG on Tabular Databases in Python

send messages to: db-sig@python.org
administrivia to: db-sig-request@python.org