dict: keys() and values() order guaranteed to be same?
Stefan Behnel
stefan_ml at behnel.de
Mon Jul 23 07:58:37 EDT 2012
Philipp Hagemeister, 23.07.2012 13:40:
> On 07/23/2012 01:23 PM, Henrik Faber wrote:
>> With an arbitrary dictionaty d, are d.keys() and d.values()
>> guaraneed to be in the same order?
>
> Yes. From the documentation[1]:
>
> If items(), keys(), values(), iteritems(), iterkeys(), and itervalues()
> are called with no intervening modifications to the dictionary, the
> lists will directly correspond.
>
> [1] http://docs.python.org/library/stdtypes.html#dict.items
Interesting. I wonder if other implementations like Jython and PyPy really
adhere to this official guarantee. At least Jython has the same paragraph
in its documentation and I would expect that PyPy follows it as well.
http://www.jython.org/docs/library/stdtypes.html#mapping-types-dict
Maybe this guarantee is just easy enough to build on the given
implementation details of a platform that it's a common property. Iteration
over data structures should tend to be deterministic, after all.
Stefan
More information about the Python-list
mailing list