OrderedDict In Python

180

OrderedDict in Python is a subclass of Dictionary and is very useful many times for programmers. This preserves the order of the keys in that they were inserted. The other available subclass is dict() but this is not able to preserve the order the keys were inserted.

OrderedDict in Python
OrderedDict in Python

We will understand these two with the help of examples given below –

from collections import OrderedDict
print("This is a Dict ordered:\n")
dic = {}
dic['one'] = 1
dic['two'] = 2
dic['three'] = 3
dic['four'] = 4
for key, value in dic.items():
   print(key, value)
print("\nThis is an Ordered Dict Order:\n")
odic = OrderedDict()
odic['one'] = 1
odic['two'] = 2
odic['three'] = 3
odic['four'] = 4

for key, value in odic.items():
   print(key, value)

Output –

This is a Dict ordered:

one 1
two 2
three 3
four 4

This is an Ordered Dict Order:

one 1
two 2
three 3
four 4

Altering in Values

Deleting, and then re-inserting a key Value:

When a key is deleted and re-inserted, it is pushed to the last.

We will understand this with the help of the example given below –

from collections import OrderedDict

print("Before deleting from OrderedDict:\n")
odic = OrderedDict()
odic['o'] = 1
odic['t'] = 2
odic['th'] = 3
odic['f'] = 4

for key, value in odic.items():
   print(key, value)

print("\nAfter deleting th from OrderedDict:\n")
odic.pop('th')
for key, value in odic.items():
   print(key, value)

print("\nAfter re-inserting th to OrderedDIct:\n")
odic['th'] = 3
for key, value in odic.items():
   print(key, value)

Output –

Before deleting from OrderedDict:

o 1
t 2
th 3
f 4

After deleting th from OrderedDict:

o 1
t 2
f 4

After re-inserting th to OrderedDIct:

o 1
t 2
f 4
th 3

You may also like: Inheritance in Python

Change in Key Value:

If a key value is changed in orderedDict, the position of the key still remains the same as before.

 We will understand this with the help of the example given below –

from collections import OrderedDict

print("Before:\n")
odic = OrderedDict()
odic['one'] = 1
odic['two'] = 2
odic['three'] = 3
odic['four'] = 4
for key, value in odic.items():
   print(key, value)

print("\nAfter:\n")
odic['five'] = 5
for key, value in odic.items():
   print(key, value)

Output –

Before:

one 1
two 2
three 3
four 4

After:

one 1
two 2
three 3
four 4
five 5

So, in this article, we learned about one of the very useful sub-class of Dictionary class which was orderedDict. Which is used when we want the ordered to be maintained. This is quite similar to the ordered List that we learn in the C++ STL library.

Attempt free python online test

Previous articleUser defined exceptions in Python
Next articlePolymorphism in Python
Harshit brijwasi Yuvayana
I am Harshit Brijwasi. I am an engineer and a Technical Content writer. I am here to share my knowledge about technical topics and help researchers with them.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.