dictionary python are they ordered code example
Example 1: ordered dictionary python
from collections import OrderedDict
x = OrderedDict(a=1, b=2, c=3)
Example 2: how to make an ordered dictionary in python
from collections import OrderedDict
print("Before:\n")
od = OrderedDict()
od['a'] = 1
od['b'] = 2
od['c'] = 3
od['d'] = 4
for key, value in od.items():
print(key, value)
print("\nAfter:\n")
od['c'] = 5
for key, value in od.items():
print(key, value)
"""
Ouptut:
Before:
('a', 1)
('b', 2)
('c', 3)
('d', 4)
After:
('a', 1)
('b', 2)
('c', 5)
('d', 4)
"""