Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds
key:value pair. Key-value is provided in the dictionary to make it more optimized. A regular dictionary type does not track the insertion order of the (key, value) pairs and thus iterates through the keys based on how they are stored in the hash table which in turn is based on random values so as to reduce collisions.
In contrast to this Python provides the OrderedDict type which remembers the insertion order of (key, value) pairs in the dictionary and thus preserves the order. OrderedDict consumes more memory than a regular dictionary in Python because of the underlying Doubly LinkedList implementation to preserving the order.
Regular dictionary: ('a', 97) ('c', 99) ('b', 98) ('e', 101) ('d', 100) ('f', 102) OrderedDict: ('a', 97) ('b', 98) ('c', 99) ('d', 100) ('e', 101) ('f', 102)
Note: Starting from Python 3.7, insertion order of Python dictionaries is guaranteed.
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.
Before deleting: Regular dictionary: ('a', 1) ('c', 3) ('b', 2) ('d', 4) Ordered dictionary: ('a', 1) ('b', 2) ('c', 3) ('d', 4) After deleting: Regular dictionary: ('a', 1) ('b', 2) ('d', 4) Ordered dictionary: ('a', 1) ('b', 2) ('d', 4) After re-inserting: Regular dictionary: ('a', 1) ('c', 3) ('b', 2) ('d', 4) Ordered dictionary: ('a', 1) ('b', 2) ('d', 4) ('c', 3)
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Scraping And Finding Ordered Words In A Dictionary using Python
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Methods of Ordered Dictionary in Python
- NLP | Storing an ordered dictionary in Redis
- Python - Ordered tuples extraction
- Python - Filter dictionaries with ordered values
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
- Find all the numbers in a string using regular expression in Python
- Check the equality of integer division and math.floor() of Regular division in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.