dict: keys() and values() order guaranteed to be same?

Henrik Faber hfaber at invalid.net
Mon Jul 23 13:23:01 CEST 2012


Hi group,

I have a question of which I'm unsure if the specification guarantees
it. With an arbitrary dictionaty d, are d.keys() and d.values()
guaraneed to be in the same order? I.e. what I mean is:

# For all dictionaries d:
assert({ list(d.keys())[i]: list(d.values())[i] for i in range(len(d)) }
== d)

I'm curious if it's allowed because in a special case it would make for
a nice shortcut and clean code. I think however that the implementation
may chose not to have them in the same order necessarily -- then I'd
obviously avoid relying on it.

Best regards,
Joe



More information about the Python-list mailing list