d.keys() and d.values()

Max M maxm at mxm.dk
Tue Jun 24 08:53:47 EDT 2003


Gerrit Holl wrote:
 > Hi,
 >
 > is it guaranteed that dict(zip(d.keys(), d.values())) == d?
 > In words, do .keys() and .values() always have the same order? Is
 > it safe to rely on this?

It says so in the docs.

http://www.python.org/doc/current/lib/typesmapping.html

(3) "Keys and values are listed in random order. If keys() and values() 
are called with no intervening modifications to the dictionary, the two 
lists will directly correspond. This allows the creation of (value, key) 
pairs using zip(): "pairs = zip(a.values(), a.keys())". "


regards Max M





More information about the Python-list mailing list