English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
OrderedDict is a subclass of the dict object in Python. The only difference between OrderedDict and dict is that in OrderedDict, it maintains the order of inserted keys. In a dictionary, sorting may or may not occur.
OrderedDict is a standard library class located incollectionsModule.
Firstly, we need to import it to use it.SetStandard library module.
import collections
In this section, we will see some operations related to OrderedDict and the differences between OrderedDict and Dict.
We can put some keys and values into Dict and OrderedDict. In this example, we can see that the order of Dict may be different. However, for OrderedDict, it will be fixed.
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 Result
('AA', 11) ('CC', 33) ('BB', 22) ('DD', 44) ('AA', 11) ('BB', 22) ('CC', 33) ('DD', 44)
For OrderedDict, the order of keys will not change when the value of a specified key is changed. We can see that this statement may be correct or incorrect for Dict type objects.
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 Result
('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)
When an element is deleted from an OrderedDict and the key and value are reinserted, it will push it back to the end. Although it maintains the order when inserting, it will delete the ordering information and be reinserted as a new entry when deleting.
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 Result
('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)