Home »
Python
OrderedDict in Python
Python | OrderedDict: Here, we will learn about the OrderedDict with examples in Python programming language.
Submitted by Shivang Yadav, on May 27, 2021
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Dictionary is a collection in python which is used to store data as Key:value pair.
Example:
dict = { "python" : 1, "C++" : 3, "javaScript" : 2 }
OrderedDict
OrderedDict is a special type of dictionary in Python in which the order of insertion of key-value pairs is preserved.
The OrderedDict collection is imported in Python from the collections library as OrderedDict.
Import syntax:
from collections import OrderedDict
Syntax for creating a new OrderedDict
OrderedDict_Name = OrderedDict([(key1, value1), (key2, value2), ...])
Python Program to create an OrderedDict
# Python program to create an OrderedDict
from collections import OrderedDict
# Creating a OrderedDict
myOrderedDict = OrderedDict([('javascript', '4'), ('python', '12'), ('R', '7')])
# Printing
print("Ordered dictionary : ", str(myOrderedDict))
Output:
Ordered dictionary : OrderedDict([('javascript', '4'), ('python', '12'), ('R', '7')])
Difference b/w Regular Dictionary and Ordered Dictionary
There is a major difference between a regular dictionary and OrderedDict in the order of insertion. It is the fact that there is no order of insertion present in a regular dictionary but in the case of OrderedDict, the order in which the key-value pairs are inserted remains the same.
Let's a program which compares both types of dictionaries...
# Python program for difference b/w
# regular dictionary and Ordered dictionary
from collections import OrderedDict
# Creating a regular dictionary
myDict = {'javascript': 4, 'python': 12, 'R':7 }
print("Regular dictionary : ", str(myDict))
# Creating a OrderedDict
myOrderedDict = OrderedDict([('javascript', 4), ('python', 12), ('R', 7)])
print("Ordered dictionary : ", str(myOrderedDict))
Output:
Regular dictionary : {'R': 7, 'javascript': 4, 'python': 12}
Ordered dictionary : OrderedDict([('javascript', 4), ('python', 12), ('R', 7)])
Effect of changing values on the order of OrderedDict
If we change the value of a key-value pair of an OrderedDict, the order will remain the same after the change.
Python program to illustrate the concept
# Python program for difference b/w
# regular dictionary and Ordered dictionary
from collections import OrderedDict
# Creating a OrderedDict
myOrderedDict = OrderedDict([('javascript', 4), ('python', 12), ('R', 7)])
print("Ordered dictionary : ", str(myOrderedDict))
# Changing the value of a key-value pair
myOrderedDict['python'] = 1
# printing values
print("Updated Ordered dictionary : ", str(myOrderedDict))
Output:
Ordered dictionary : OrderedDict([('javascript', 4), ('python', 12), ('R', 7)])
Updated Ordered dictionary : OrderedDict([('javascript', 4), ('python', 1), ('R', 7)])
ADVERTISEMENT
ADVERTISEMENT