MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor

Petr Messner petr.messner at gmail.com
Thu Jul 2 13:48:28 EDT 2009


2009/7/2 Tim Chase <python.list at tim.thechases.com>:
>> Will this order at least be the same for that same query every time the
>> script is executed?
>
> I wouldn't count on it.  The order is only defined for the one iteration
> (result of the keys() call).  If the order matters, I'd suggest a
> double-dispatch with a non-dict (regular/default) query result, something
...

Dictionaries are usually designed to have the best performance when
accessing certain key, so it is not expected that dictionary items
will be in any particular order; this applies not only for Python. But
(for Python dict) you can be sure that results of items(), keys(),
values(), iteritems(), iterkeys(), and itervalues() will correspond if
called with no intervening modifications to the dictionary.

If such a functionality is needed, there is a collections.OrderedDict
that was introduced in Python 3.1; there is also an implementation for
Python 2.4 or later: http://code.activestate.com/recipes/576693/ (this
links is from "What’s New In Python 3.1")

PM



More information about the Python-list mailing list