In Python, OrderedDict is a subclass of dictionary that remembers the order of the keys that were inserted. The only difference between OrderedDict() and the dict() lies in their handling of key order.
The standard dictionary does not guarantee any specific order when iterated whereas, the OrderedDict provides a specific order in which keys are added into the dictionary and maintain the same order when iterated.
SyntaxFollowing is the syntax of the Python OrderedDict() class −
collections.OrderedDict()Parameters
It accepts dictionary as a parameter.
Return ValueIt returns ordered dictionary.
ExampleFollowing is an basic example of the Python OrderedDict() −
# A Python program to demonstrate working of OrderedDict from collections import OrderedDict od = OrderedDict() od['a'] = 1 od['b'] = 2 od['c'] = 3 od['d'] = 4 for key, value in od.items(): print(key,":",value)
Following is the output of the above code −
a : 1 b : 2 c : 3 d : 4Key value change in OrderedDict
We can change the value of the keys but the positions remain unchanged in the OrderedDict().
ExampleHere, we have defined an OrderedDict() and changed the key value but the order remainded same −
# A Python program to demonstrate working of key # value change in OrderedDict from collections import OrderedDict od = OrderedDict() od['a'] = 1 od['b'] = 2 od['c'] = 3 od['d'] = 4 print("The Original OrderedDict :",od) od['c'] = 5 print("The OrderedDict after changing key value :",od)
Following is the output of the above code −
The Original OrderedDict : OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)]) The OrderedDict after changing key value : OrderedDict([('a', 1), ('b', 2), ('c', 5), ('d', 4)])Equality Comparison in OrderedDict
We can compare two OrderedDict() using equality[==] operator. This operator checks whether the keys and values of the both the OrderedDict are same or not. Not only keys and values but also the order of the items is checked in the OrderedDict().
ExampleIn the following example, we have compared two OrderedDict() as these dictionaries are not equal it returned False −
from collections import OrderedDict # Create two ordered dictionaries with different orderings od1 = OrderedDict([('one', 1), ('two', 2), ('three', 3)]) od2 = OrderedDict([('three', 3), ('two', 2), ('one', 1)]) # Compare the ordered dictionaries for equality print("Comparison of the two OrderedDict :",od1 == od2)
Following is the output of the above code −
Comparison of the two OrderedDict : FalseUsage of move_to_end() and update()
We can update the OrderedDict using update() method. Using move_to_end() we can move the key to end of the dictionary. It accepts key and last argument. If the last is True the key is moved to the end of the dictionary else, moved to the beginning of the dictionary.
ExampleHere, we have defined OrderedDict and updated two values as the last is False the key is moved to the beginning of the OrderedDict() −
from collections import OrderedDict # Create an ordered dictionary of key-value pairs my_dict = OrderedDict([('one', 1), ('two', 2)]) # Add a new item to the end of the dictionary my_dict['four'] = 4 # Add a new item at a specific position in the dictionary my_dict.update([('five', 5)]) my_dict.move_to_end('five', last=False) print(my_dict)
Following is the output of the above code −
OrderedDict([('five', 5), ('one', 1), ('two', 2), ('four', 4)])
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4