For two dictionaries d1
and d2
defined as
d1 = {'foo':123, 'bar':789}
d2 = {'bar':789, 'foo':123}
The order of the keys is preserved in Python 3.6+. This is evident when we loop through the dictionary and print the items.
>>> for x in d1.items():
... print(x)
...
('foo', 123)
('bar', 789)
>>> for x in d2.items():
... print(x)
...
('bar', 789)
('foo', 123)
Why does Python still consider d1
and d2
to be equal?
>>> d1 == d2
True