Everywhere I search, they say python dictionary's doesn't have any order. When I run code 1 each time shows a different output (random order). But when I run code 2 it always shows the same sorted output. Why is the dictionary ordered in the second snippet?
#code 1
d = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
for a, b in d.items():
print(a, b)
#code 2
d = {1: 10, 2: 20, 3: 30, 4: 40}
for a, b in d.items():
print(a, b)
Outputs
code 1:
four 4
two 2
three 3
one 1
code 1 again:
three 3
one 1
two 2
four 4
code 2 (always):
1 10
2 20
3 30
4 40