Dict ordering [was: Re: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor]
tim.wintle at teamrubber.com
Thu Jul 2 18:49:48 CEST 2009
On Thu, 2009-07-02 at 10:32 -0500, Wells Oliver wrote:
> for row in cursor.fetchall():
> print row.keys()
> What I get is:
> ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER']
> Neither alphabetical nor the order in which they were specified in the
> query nor... any seeming order I can suss out. Any ideas? Thanks!
keys in a dict are not guaranteed to be in any specific order. It's not
specific to the MySQLdb cursor.
IIRC it's related to the hashes of the keys (but obviously not in
strictly increasing order). You definitely shouldn't rely on the
ordering (hence the need for an OrderedDict.
>>> a = ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER']
>>> for s in a:
... print hash(s)
More information about the Python-list