If I have in a particular program two different dictionaries with the same keys (but different values), will .keys() be in the same order? I made a few tests and it seems to be the case, but without knowing are how the internals of the dict I am not sure if this is guaranteed.
Thanks,
解决方案
You cannot rely on the key order at all:
>>> {1: None, 9: None}
{1: None, 9: None}
>>> {9: None, 1: None}
{9: None, 1: None}
>>> {1: None, 2: None}
{1: None, 2: None}
>>> {2: None, 1: None}
{1: None, 2: None}
Dictionaries are unordered. In Python 2.7, there is collections.OrderedDict, though.