Python OrderedDict

OrderedDict is a subclass of the dictionary, which maintains the order in which the elements/items are added to it.

OrderedDict preserves the order of insertion of the elements. A default dict does not save the order and results in the iteration in an arbitrary order.

Initially, we need to import the collections module to use the OrderedDict library module. We can also import only the OrderedDict class from the collections module.

import collections

from collections import OrderedDict

Python OrderedDict functionalities

  • Creation of OrderedDict object
  • Adding items to OrderedDict
  • Replacing items from OrderedDict
  • Removal of items from OrderedDict
  • Key-Value Change
  • move_to_end() function
  • OrderedDict pop item
  • Reverse iteration
  • OrderedDict Equality Test

1. Creation of OrderedDict object

The OrderedDict() function is used for the creation of the object.

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
ordered_input = OrderedDict(my_input)
print(ordered_input)

Output:

OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])

2. Adding items to OrderedDict

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
ordered_input = OrderedDict(my_input)
#print(ordered_input)

print("Adding item to OrderedDict....")
ordered_input['Hyderabad'] = 'Karnataka'
print(ordered_input)

Output:

Adding item to OrderedDict....
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar'), ('Hyderabad', 'Karnataka')])

3. Replacing items from OrderedDict

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
ordered_input = OrderedDict(my_input)
#print(ordered_input)

print("Replacing item from OrderedDict....")
ordered_input['Pune'] = 'Satara'
print(ordered_input)

Output:

Adding items to OrderedDict....
OrderedDict([('Pune', 'Satara'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])

4. Removal of items from OrderedDict

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
ordered_input = OrderedDict(my_input)
#print(ordered_input)

print('Removal of item from OrderedDict....')
ordered_input.pop('Pune')
print(ordered_input)

Output:

Removal of item from OrderedDict....
OrderedDict([('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])

5. Key-Value change in an OrderedDict

In an OrderedDict, if the value corresponding to a particular key is altered, the position/index of that key remains unchanged.

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
print('Before the change.....')
ordered_input = OrderedDict(my_input)
print(ordered_input)

print('After the change.....')
ordered_input['Pune'] = 'Kiara'
print(ordered_input)

Output:

Before the change.....
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
After the change.....
OrderedDict([('Pune', 'Kiara'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])

6. move_to_end() function

The move_to_end() function moves a particular key-value pair to the end of the dict.

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
print('Before using the move_to_end().....')
ordered_input = OrderedDict(my_input)
print(ordered_input)

print('After using the move_to_end().....')
ordered_input.move_to_end('Pune')
print(ordered_input)

Output:

Before using the move_to_end().....
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
After using the move_to_end().....
OrderedDict([('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar'), ('Pune', 'Maharashtra')])

7. OrderedDict popitem

This function pops out and returns the last element as output.

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
print('Original input dict.....')
ordered_input = OrderedDict(my_input)
print(ordered_input)


result = ordered_input.popitem(True)
print('The popped item is: ')
print(result)

print(ordered_input)

Output:

Original input dict.....
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
The popped item is: 
('Orrisa', 'Bhubhaneshwar')
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat')])

8. Reverse iteration

from collections import OrderedDict


my_input = {'Pune': 'Maharashtra', 'Ahemadnagar': 'Gujarat', 'Orrisa': 'Bhubhaneshwar'}

# creating ordered dict from dict
print('Original input dict.....')
ordered_input = OrderedDict(my_input)
print(ordered_input)

print('Reversed OrderedDict.....')

for elements in reversed(ordered_input):
    print(elements)

Output:

Original input dict.....
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
Reversed OrderedDict.....
Orrisa
Ahemadnagar
Pune

9. OrderedDict Equality Test

from collections import OrderedDict

# creating regular dict..
my_input1 = {1:'one' , 2:'two'}
my_input2 = {2:'two' , 1:'one'}

#creating ordereddict..
ordered_input1 = OrderedDict({1:'one' , 2:'two'})
ordered_input2 = OrderedDict({2:'two' , 1:'one'})

print(my_input1 == ordered_input1)
print(my_input1 == my_input2)
print(ordered_input1 == ordered_input2)

Output:

True
True
False

Conclusion

Thus, in this article, we have understood the difference between regular Dictionary and OrderedDict and have had a look at the functionalities offered by OrderedDict.


References