Python orderedDict
An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that:
OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.
Example 1:
Output:
('a', 1)
('c', 3)
('b', 2)
('d', 4)
This is an Ordered Dict:
('a', 1)
('b', 2)
('c', 3)
('d', 4)
Important Points:
Key value Change: If the value of a certain key is changed, the position of the key remains unchanged in OrderedDict.
dict:
True
Ordered Dic:
False
Last updated
Was this helpful?