An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that:
OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.
# A Python program to demonstrate working of OrderedDict from collections import OrderedDict print("This is a Dict:\n") d = {} d['a'] = 1d['b'] = 2d['c'] = 3d['d'] = 4 for key, value in d.items(): print(key, value) print("\nThis is an Ordered Dict:\n") od = OrderedDict() od['a'] = 1od['b'] = 2od['c'] = 3od['d'] = 4 for key, value in od.items(): print(key, value) |
Output:
This is a Dict:
('a', 1)
('c', 3)
('b', 2)
('d', 4)
This is an Ordered Dict:
('a', 1)
('b', 2)
('c', 3)
('d', 4)
Important Points:
- Key value Change: If the value of a certain key is changed, the position of the key remains unchanged in OrderedDict.
# A Python program to demonstrate working of key# value change in OrderedDictfromcollectionsimportOrderedDictprint("Before:\n")od=OrderedDict()od['a']=1od['b']=2od['c']=3od['d']=4forkey, valueinod.items():print(key, value)print("\nAfter:\n")od['c']=5forkey, valueinod.items():print(key, value)chevron_rightfilter_noneOutput:
Before: ('a', 1) ('b', 2) ('c', 3) ('d', 4) After: ('a', 1) ('b', 2) ('c', 5) ('d', 4) - Deletion and Re-Inserting: Deleting and re-inserting the same key will push it to the back as OrderedDict however maintains the order of insertion.
# A Python program to demonstrate working of deletion# re-inserion in OrderedDictfromcollectionsimportOrderedDictprint("Before deleting:\n")od=OrderedDict()od['a']=1od['b']=2od['c']=3od['d']=4forkey, valueinod.items():print(key, value)print("\nAfter deleting:\n")od.pop('c')forkey, valueinod.items():print(key, value)print("\nAfter re-inserting:\n")od['c']=3forkey, valueinod.items():print(key, value)chevron_rightfilter_noneOutput:
Before deleting: ('a', 1) ('b', 2) ('c', 3) ('d', 4) After deleting: ('a', 1) ('b', 2) ('d', 4) After re-inserting: ('a', 1) ('b', 2) ('d', 4) ('c', 3)
Other Considerations:
- Ordered dict in Python version 2.7 consumes more memory than normal dict. This is due to the underlying Doubly Linked List implementation for keeping the order. In Python 2.7 Ordered Dict is not dict subclass, it’s a specialized container from collections module.
- Starting from Python 3.7, insertion order of Python dictionaries is guaranteed.
- Ordered Dict can be used as a stack with the help of popitem function. Try implementing LRU cache with Ordered Dict.
This article is contributed by Sri Sanketh Uppalapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Python | Check order of character in string using OrderedDict( )
- LRU Cache in Python using OrderedDict
- Python - Insertion at the beginning in OrderedDict
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
Improved By : nickzuck_007

