Python collections OrderedDict() Method with Examples

OrderedDict:

An OrderedDict is a subclass of dictionary that remembers the order in which keys were first placed. The only distinction between dict() and OrderedDict() is:

The sequence in which the keys are inserted is preserved by OrderedDict. A standard dict does not keep track of the insertion order, therefore iterating through it returns the items in any order. OrderedDict, on the other hand, remembers the order in which the elements are placed.

Collections OrderedDict() Method with Examples in Python

i)OrderedDict vs Normal Dictionary Example

Approach:

  • Import the OrderedDict from collections using the import keyword.
  • Take a Empty dictionary using {} or dict() (Normal Dictionary).
  • Give some random keys and values for the above dictionary.
  • Take another dictionary using OrderedDict() (Ordered Dictionary).
  • Give the same keys and values for this dictionary.
  • Print the Normal Dictionary and Ordered Dictionary keys and Values.
  • We can see that the ordered is not maintained in Normal Dictionary but ordered is maintained in Ordered Dictionary.
  • The Exit of the Program.

Below is the Implementation:

# Import the OrderedDict from collections using the import keyword.
from collections import OrderedDict
# Take a Empty dictionary using {} or dict() (Normal Dictionary).
normalDict = {}
# Give some random keys and values for the above dictionary.
normalDict['hello'] = 100
normalDict['this'] = 400
normalDict['is'] = 500
normalDict['Python-Programs'] = 900
# Take another dictionary using OrderedDict() (Ordered Dictionary).
orderedDictionary = OrderedDict()
# Give the same keys and values for this dictionary.
orderedDictionary['hello'] = 100
orderedDictionary['this'] = 400
orderedDictionary['is'] = 500
orderedDictionary['Python-Programs'] = 900
# Print the Normal Dictionary and Ordered Dictionary keys and Values.
# We can see that the ordered is not maintained in Normal Dictionary but ordered is maintained in Ordered Dictionary.
print('Normal Dictionary :')
print(normalDict)
print('Ordered Dictionary : ')
print(orderedDictionary)

Output:

Normal Dictionary :
{'hello': 100, 'is': 500, 'this': 400, 'Python-Programs': 900}
Ordered Dictionary : 
OrderedDict([('hello', 100), ('this', 400), ('is', 500), ('Python-Programs', 900)])

ii)What happens if we change/modify key or value in Ordered Dictionary?

In OrderedDict, if the value of a key is altered, the key’s position remains unchanged.

Approach:

  • Import the OrderedDict from collections using the import keyword.
  • Take a dictionary using OrderedDict() (Ordered Dictionary).
  • Give some random keys and values for this dictionary.
  • Print the Dictionary before modification
  • Modify any key in the Above Dictionary by changing its value.
  • Print the Dictionary after modification
  • The Exit of the Program.

Below is the Implementation:

# Import the OrderedDict from collections using the import keyword.
from collections import OrderedDict

# Take a dictionary using OrderedDict() (Ordered Dictionary).
orderedDictionary = OrderedDict()
# Give some random keys and values for this dictionary.
orderedDictionary['hello'] = 100
orderedDictionary['this'] = 400
orderedDictionary['is'] = 500
orderedDictionary['Python-Programs'] = 900
# Print the Dictionary before modification
print('The given Dictionary Before Modification : ')
for key, value in orderedDictionary.items():
    print(key, value)
# Modify any key in the Above Dictionary by changing its value.
orderedDictionary['is'] = 1929
# Print the Dictionary after modification
print('The given Dictionary After Modification : ')
for key, value in orderedDictionary.items():
    print(key, value)

Output:

The given Dictionary Before Modification : 
hello 100
this 400
is 500
Python-Programs 900
The given Dictionary After Modification : 
hello 100
this 400
is 1929
Python-Programs 900

Explanation:

Here we can see that the order remains unchanged.

iii)What if we remove and reinsert the same key in Ordered Dictionary?

As OrderedDict maintains the order of insertion, deleting and re-inserting the same key will move it to the rear.

Approach:

  • Import the OrderedDict from collections using the import keyword.
  • Take a dictionary using OrderedDict() (Ordered Dictionary).
  • Give some random keys and values for this dictionary.
  • Print the Dictionary before Deleting the key.
  • Remove Some Random Key from the above dictionary using the pop() method.
  • Print the Dictionary after Deleting the key.
  • Insert the same key again to the Ordered Dictionary
  • Print the Dictionary after Reinserting the same key.
  • The Exit of the Program.

Below is the Implementation:

# Import the OrderedDict from collections using the import keyword.
from collections import OrderedDict

# Take a dictionary using OrderedDict() (Ordered Dictionary).
orderedDictionary = OrderedDict()
# Give some random keys and values for this dictionary.
orderedDictionary['hello'] = 100
orderedDictionary['this'] = 400
orderedDictionary['is'] = 500
orderedDictionary['Python-Programs'] = 900
# Print the Dictionary before Deleting the key.
print('The given Dictionary Before Deletion : ')
for key, value in orderedDictionary.items():
    print(key, "->", value)
# Remove Some Random Key from the above dictionary using the pop() method.
orderedDictionary.pop('is')
# Print the Dictionary after Deleting the key.
print('The given Dictionary After Deletion : ')
for key, value in orderedDictionary.items():
    print(key, "->", value)
# Insert the same key again to the Ordered Dictionary
orderedDictionary['is'] = 500
# Print the Dictionary after Reinserting the same key.
print('The given Dictionary After Reinserting the same key : ')
for key, value in orderedDictionary.items():
    print(key, "->", value)

Output:

The given Dictionary Before Deletion : 
hello -> 100
this -> 400
is -> 500
Python-Programs -> 900
The given Dictionary After Deletion : 
hello -> 100
this -> 400
Python-Programs -> 900
The given Dictionary After Reinserting the same key : 
hello -> 100
this -> 400
Python-Programs -> 900
is -> 500