Yes. Per http://docs.python.org/library/stdtypes.html#dict.items :
"""
Note: Keys and values are listed in an arbitrary order which is
non-random, varies across Python implementations, and depends on the
dictionary’s history of insertions and deletions. If items(), keys(),
values(), iteritems(), iterkeys(), and itervalues() are called with no
intervening modifications to the dictionary, the lists will directly
correspond. This allows the creation of (value, key) pairs using
zip(): pairs = zip(d.values(), d.keys()).
"""
Cheers,
Chris
--
http://blog.rebertia.com
> I understand that the keys in a dictionary are ordered not randomly but
> something practically close to it, but if I create a SQL query like so:
>
> query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()),
> ','.join(stats.values()))
>
> Can I at least rely on the value being in the same index as its
> corresponding key?
You already got the answer you wanted, but note that building a SQL
statement that way is unsafe [1]. I prefer this way:
query= 'INSERT INTO Batting (%s) VALUES(%s)' % (
','.join(stats.keys()),
','.join(['?']*len(stats)))
cursor.execute(query, stats.values())
[1] If you don't know what "SQL injection" means, see http://xkcd.com/327/
--
Gabriel Genellina
I love how XKCD is one of the preferred learning tools (along with
Wikipeida) for people on this list. I think Randall Munroe should make
a comic about it. :)