This question arises from this answer where one user uses
d.keys()
andd.values()
separately to initialise a dataframe.
It's common knowledge that dictionaries in python versions under 3.6 are not ordered.
Consider a generic dictionary of the form:
d = {k1 : v1, k2 : v2, k3 : v3}
Where the keys k*
are any hashable objects, and the values v*
being any object. Of course, order cannot be guaranteed, but what about the order of d.keys()
and d.values()
?
Python 2.x
Both d.keys()
and d.values()
return lists. Say, .keys()
returns d
's keys in the order [k2, k1, k3]
. Is it now always guaranteed that d.values()
returns the same relative ordering as [v2, v1, v3]
? Furthermore, does the ordering remain the same no matter how many times these functions are called?
Python 3.x (<3.6)
I'm not 100% sure, but I believe that .keys
and .values
do not guarantee any ordering at all here because they are set-like structures, thus having no order by definition and enabling you to perform set-like operations on them. But I'd still be interested to know if there is any sort of relative ordering between the two calls in this instance. I'm guessing not. I'd appreciate if someone could affirm or correct me.
Yes, what you observed is indeed a guaranteed property — keys() , values() and items() return lists in congruent order if the dict is not altered.
values() changes depend on the data in it. That's why it is called unordered. Althought I stated the 'b' key first, it was allocated after 'a' . However, once the dict is set, it will always return the same order when called dict.
Answer. No, there is no guaranteed order for the list of keys returned by the keys() function.
Standard dict objects preserve order in the reference (CPython) implementations of Python 3.5 and 3.6, and this order-preserving property is becoming a language feature in Python 3.7.
The general rules:
d.keys()
, d.values()
, and d.items()
all return the elements in a respective order. The order should be treated as arbitrary (no assumptions should be made about it). (docs)d.keys()
, d.values()
, and d.items()
are "stable", in the sense they are guaranteed to preserve the order of previous calls (assuming no insertion/deletion happens between the calls).dict
has been reimplemented, and it now preserves insertion order. This was not the goal of the change, but a side effect, and it is NOT part of the python spec, only a detail of the CPython implementation. See point #1 above: relying on this is bad practice and should not be done. Anyways, you should avoid writing CPython-specific code.EDIT: added point #5, thanks to @AndyHayden's comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With