OrderedDict in Python

PythonProgrammingServer Side Programming

The OrderedDict is a subclass of dict object in Python. The only difference between OrderedDict and dict is that, in OrderedDict, it maintains the orders of keys as inserted. In the dict, the ordering may or may not be happen.

The OrderedDict is a standard library class, which is located in the collections module.

To use it at first we need to import it the collections standard library module.

import collections

In this section we will see some operations on OrderedDictand difference between OrderedDict and Dict.

We can put some keys and values in the Dict and OrderedDict. In this example we can see that the ordering of the Dict may vary. But for the OrderedDict. It will be fixed.

Example Code

import collections
#Create normal dict
my_dict = {}
my_dict['AA'] = 11
my_dict['BB'] = 22
my_dict['CC'] = 33
my_dict['DD'] = 44
for item in my_dict.items():
   print(item)
print()
#Create ordered dict
my_ord_dict = collections.OrderedDict()
my_ord_dict['AA'] = 11
my_ord_dict['BB'] = 22
my_ord_dict['CC'] = 33
my_ord_dict['DD'] = 44
for item in my_ord_dict.items():
   print(item)

Output

('AA', 11)
('CC', 33)
('BB', 22)
('DD', 44)

('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)

Changing the value of a specified key

When the value of a specified key is changed, the ordering of keys will not change for the OrderedDict. We can see that the statement may or may not be true for the Dict type objects.

Example Code

import collections
#Create normal dict
my_dict = {}
my_dict['AA'] = 11
my_dict['BB'] = 22
my_dict['CC'] = 33
my_dict['DD'] = 44
for item in my_dict.items():
   print(item)
#Change the value for key BB
my_dict['BB'] = 100
print('After changing in Dict')
for item in my_dict.items():
   print(item)
print()
#Create ordered dict
my_ord_dict = collections.OrderedDict()
my_ord_dict['AA'] = 11
my_ord_dict['BB'] = 22
my_ord_dict['CC'] = 33
my_ord_dict['DD'] = 44
for item in my_ord_dict.items():
   print(item)
#Change the value for key BB
my_ord_dict['BB'] = 100
print('After changing in Ordered Dict')
for item in my_ord_dict.items():
   print(item)

Output

('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)
After changing in Dict
('AA', 11)
('CC', 33)
('DD', 44)
('BB', 100)

('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)
After changing in Ordered Dict
('AA', 11)
('BB', 100)
('CC', 33)
('DD', 44)

Deleting and Reinserting the elements in OrderedDict

When one element is deleted from OrderedDict and perform the reinserting operation on that key and value, it will push it to the back. Though it was maintaining the order while inserting, but for deletion, it removes the ordering information, and treat as new entry for re inserting.

Example Code

import collections
#Create ordered dict
my_ord_dict = collections.OrderedDict()
my_ord_dict['AA'] = 11
my_ord_dict['BB'] = 22
my_ord_dict['CC'] = 33
my_ord_dict['DD'] = 44
for item in my_ord_dict.items():
   print(item)
#Delete item in key BB
my_ord_dict.pop('BB')
print('After Deleting')
for item in my_ord_dict.items():
   print(item)
#re-inserting item 
my_ord_dict['BB'] = 22
print('After Re-inserting')
for item in my_ord_dict.items():
   print(item)    

Output

('AA', 11)
('BB', 22)
('CC', 33)
('DD', 44)
After Deleting
('AA', 11)
('CC', 33)
('DD', 44)
After Re-inserting
('AA', 11)
('CC', 33)
('DD', 44)
('BB', 22)
raja
Published on 06-Nov-2018 17:34:31
Advertisements