Starting from 3.7, standard python dictionaries are guaranteed to maintain the insertion order. (*)
d = {'b': 1, 'a': 2}
for k in d:
print(k)
# Prints always 'b' before 'a'.
In other words, the dict keys are kept in a strict order. In principle, this would allow the keys to be reversible. However, none of the following works:
# TypeError: 'dict' object is not reversible
for k in reversed(d):
print(k)
# TypeError: 'dict_keys' object is not reversible
for k in reversed(d.keys()):
print(k)
Questions: What is the reasoning behind this behavior? Why have dicts not been made reversible? Are there any plans to change this behavior in future?
The workaround of course works:
for k in reversed(list(d.keys())):
print(k)
(*) As a matter of fact, this is the case already for typical installations of python 3.6, as discussed in this post.
Update: Starting with python 3.8 dicts are actually reversible. The accepted answer refers to the discussion between Guido and other core developers that led to this decision. In a nutshell, they weighted language consistency against implementation efforts and actual benefits for the users.