Python ordereddict
Python OrderedDict is a subclass of 'dict' that remembers the order in which its contents are added. A normal dict doesn't follow the insertion order, and repeating over it delivers the values in an arbitrary order. In an OrderedDict, on the other hand, the order the items are inserted is remembered and utilized while making an iterator.
Python OrderDict
As, OrderedDict is part of the Python collections module. To characterize an OrderedDict in Python, we import it from the module 'collections'. Here is the syntax -
from collections import OrderedDict
The OrderedDict() function is used for the creation of the object. We can create an empty OrderedDict and add items to it -
# creating empty ordered dict
ordered_dict = OrderedDict()
Then, we can add some key value pairs to it. The following example demonstrates this -
from collections import OrderedDict
# creating a simple dict
my_dict = {'apple': 7, 'mango': 10, 'pineapple': 3}
# creating empty ordered dict
ordered_dict = OrderedDict()
print(ordered_dict)
# creating ordered dict from dict
ordered_dict = OrderedDict(my_dict)
print(ordered_dict)
Output of the above code -
OrderedDict()
OrderedDict([('apple', 7), ('mango', 10), ('pineapple', 3)])
Adding items to OrderedDict
We can perform different operations on OrderedDict. The following example demonstrates how we can add an item to an existing OrderedDict -
from collections import OrderedDict
# creating a simple dict
my_dict = {'apple': 7, 'mango': 10, 'pineapple': 3}
# creating empty ordered dict
ordered_dict = OrderedDict()
print(ordered_dict)
# creating ordered dict from dict
ordered_dict = OrderedDict(my_dict)
print("Original OrderedDict : ",ordered_dict)
# adding elements to dict
ordered_dict['banana'] = 12
print("Modified OrderedDict : ",ordered_dict)
Output of the above code -
OrderedDict()
Original OrderedDict : OrderedDict([('apple', 7), ('mango', 10), ('pineapple', 3)])
Modified OrderedDict : OrderedDict([('apple', 7), ('mango', 10), ('pineapple', 3), ('banana', 12)])
Removing items from OrderedDict
If an element is added and deleted, then it moves to the last. In the following example, we have removed an element from the OrderedDict -
from collections import OrderedDict
# creating a simple dict
my_dict = {'apple': 7, 'mango': 10, 'pineapple': 3}
# creating empty ordered dict
ordered_dict = OrderedDict()
print(ordered_dict)
# creating ordered dict from dict
ordered_dict = OrderedDict(my_dict)
print("Original OrderedDict : ",ordered_dict)
# adding elements to dict
ordered_dict.pop('mango')
print("Modified OrderedDict : ",ordered_dict)
Output of the above code-
Original OrderedDict : OrderedDict([('apple', 7), ('mango', 10), ('pineapple', 3)])
Modified OrderedDict : OrderedDict([('apple', 7), ('pineapple', 3)])
Replacing item from OrderedDict
The following example demonstrates how to replace an element from an OrderedDict -
from collections import OrderedDict
# creating a simple dict
my_dict = {'apple': 7, 'mango': 10, 'pineapple': 3}
# creating empty ordered dict
ordered_dict = OrderedDict()
print(ordered_dict)
# creating ordered dict from dict
ordered_dict = OrderedDict(my_dict)
print("Original OrderedDict : ",ordered_dict)
# replacing elements to dict
ordered_dict['mango'] = '15'
print("Replacing item from OrderedDict")
print(ordered_dict)
Output of the above code -
OrderedDict()
Original OrderedDict : OrderedDict([('apple', 7), ('mango', 10), ('pineapple', 3)])
Replacing item from OrderedDict
OrderedDict([('apple', 7), ('mango', '15'), ('pineapple', 3)])
Related Articles
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword