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.