Updated March 31, 2023
Introduction to OrderedDict in Python
The python Ordered dictionary is very similar to a dictionary in python. There is no big difference between them. The technical representation of OrderedDict is also very similar to a python dictionary. But the key thing to notice here is that the ordered dictionary is an item which is within collections in python. So, these collection containers are basically replacement data types for python’s default data types like dict, set, etc.
Here the OrderedDict is a data type which is under the collection container dictionary. From a functionality perspective, these ordered dictionaries have the capability to remember the order in which the values are entered. So, the arrangement in which the values are present is very well realised by the OrderedDict data type. So, when a new key is added or an existing key is changed, the order will be rearranged like the newly changed key will be placed as the last item in the ordered dict datatype. This is the major difference between the ordered dictionary from a normal dictionary.
Syntax of OrderedDict in Python
Given below is the syntax mentioned:
from collections import OrderedDict
dictionary_variable = OrderedDict()
In the above syntax, first, the Ordered dictionary class is imported from the collections class. This is done by means of the import statement. Here the import statement is referenced to import the OrderedDict class from the collection’s container. Next, a variable is declared to create a dictionary variable by referring to the OrderedDict method. So, making this reference will create the OrderedDict datatype active. The users can insert new items into the ordered dictionary, and from there on, the dictionary will maintain the order of insertion. Importantly the dictionary variable used here will be operating as an OrderedDict(). One key point of ordered dict is the ordered dict will consume a large amount of energy in python version 2.7. This energy is much larger than the amount of energy consumed by a normal dictionary in the same 2.7 version. Moreover, this memory difference between the ordered dict and normal dict happens in the python 2.7 version because of the doubly linked implementation list for storing the values in a specific order to maintain the order of insertion. So using this doubly linked list makes the consumption of a larger amount of memory. More interesting python normal dictionaries itself ensure to maintain the insertion order starting from the python 3.7 versions. So, from the version of python 3.7, instead of using ordered dict for ensuring the order of insertion, normal dictionaries will itself satisfy this necessity. This was a mighty advantage of dictionaries in python 3.7 versions.
Functions in OrderedDict
Given below are the functions in OrderedDict:
1. popitem(last=True)
The pop item method is used to pop or remove the specific (key-value) pair. So, the pop item is responsible for removing an item from the dictionary. So, when one specific item needs to be removed from the dictionary, then the Ordered dict can be used. The pop item method has an argument called last; the last argument will have two values, either as True or False. When the last variable is assigned to True, then pop will operate on the last in first out principle (LIFO). Whereas when the last=False, then the pop operation will operate on the FIFO principle. So as per FIFO, when the pop is called, the First item inserted will be popped out as the First item out.
2. move_to_end(key, last=True)
The move to end method is used to move a specific key as the last item in the dictionary. So, when a key needs to be moved to a right most end or left most end, then the move to end method can be used. The move to end works in such a way that when last=true, then the key indicated will be moved to the right-side end of the dictionary. Whereas when the last=false is set, the key is targeted to the left end of the dictionary. So, the last argument determines whether the newly inserted key has to be placed at the left end or the right end of the dictionary. Functions like this bring in a lot of flexibility in getting the OrderedDict dictionary to be rearranged. This is among the major advantages of using the OrderedDict on top of a normal dictionary. The position of the keys which are present can be flexibly moved or placed within the dictionary item in the necessary position.
Example of OrderedDict in Python
Given below is the example of OrderedDict in Python:
Code:
from collections import OrderedDict
print("Normal Dicitonary:\n")
Normal_dict = {}
Normal_dict['Item1'] = 10
Normal_dict['Item2'] = 20
Normal_dict['Item3'] = 30
Normal_dict['Item4'] = 40
Normal_dict['Item3'] = 70
for item_key, item_value in Normal_dict.items():
print(item_key, item_value)
ordered_dict = OrderedDict()
ordered_dict['Item1'] = 10
ordered_dict['Item2'] = 20
ordered_dict['Item3'] = 30
ordered_dict['Item4'] = 40
ordered_dict.move_to_end('Item3')
print("\n Ordered dicitonary before pop and move to end applied:\n")
for item_key, item_value in ordered_dict.items():
print(item_key, item_value)
ordered_dict.popitem(last=False)
print("\n Ordered dicitonary after pop:\n")
for item_key, item_value in ordered_dict.items():
print(item_key, item_value)
Output:
Explanation:
- In the above example, two dictionaries are created. First, a normal python dictionary is created, and items are inserted into it and displayed. Then, the created ordered dictionary is first applied on the move to end function. So, this move to the end function applied on the third item makes the third item to be the last one in the dictionary.
- Next, the pop item method is applied. This pop item removes the first item from the dictionary since the pop is applied with argument last=false. Additionally, from the version of python 3.7, instead of using OrderedDict for ensuring the order of insertion, normal dictionaries will itself satisfy this necessity.
Conclusion
Collections like ordered dict are very useful in the implementation of LRU cache functioning. So these kinds of LRU cache functioning like items that expect arrangement of items to maintain the order of the arrangement are among the major areas where the OrderedDict functionality is widely implied. Equality tests among objects of the OrderedDict and other mapping objects are insensitive to order an arrangement like normal dictionaries. This is the major identity of the ordered dictionary with a normal dictionary.
Recommended Articles
This is a guide to OrderedDict in Python. Here we discuss the introduction, functions in OrderedDict, and example, respectively. You may also have a look at the following articles to learn more –