Home »
Python »
Python programs
Python program to insert an element at the beginning in OrderedDict
Here, we will see a Python program to insert an element entered by the user at the beginning in OrderedDict.
Submitted by Shivang Yadav, on May 22, 2021
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Dictionary is a collection in python which is used to store data as Key:value pair.
Example:
dict = { "python" : 1, "C++" : 3, "javaScript" : 2 }
Ordered Dictionary is a special type of dictionary which remembers the order of insertion of keys.
Inserting at the beginning of OrderedDict
We have an OrderedDict in Python and a key-value pair which is entered by the user. We need to add it to the beginning of the OrderedDict.
Input:
orderedDict = {'javascript' : 4, 'python' : 7, 'c' : 4} -> 'r' : 5
Output:
{'r' : 5, 'javascript' : 4, 'python' : 7, 'c' : 4}
Python provides multiple methods to insert an element to the beginning of the OrderedDict.
Let's see some methods...
Method 1:
A simple approach is by creating a new OrderedDict from the entered key value pair and then creating a new one by merging them in such a way that the entered pair comes at the beginning of this OrderedDict.
Program to insert an element at the beginning in OrderedDict
# Python program to insert an element at the beginning in OrderedDict
from collections import OrderedDict
# creating a OrderedDict
myDict = OrderedDict([('javascript', '4'), ('python', '12')])
print("Original dictionary : ", str(myDict))
print("Enter the key-value pair to be added in the OrderedDict")
key = input("key : ")
value = input("value : ")
addedDict = OrderedDict([(key, value)])
# Inserting key-value pair at the beginning of the OrderedDict
newOrderedDict = OrderedDict(list(addedDict.items()) + list(myDict.items()))
# Printing the Resultant dictionary
print ("Resultant Dictionary :"+str(newOrderedDict))
Output:
riginal dictionary : OrderedDict([('javascript', '4'), ('python', '12')])
Enter the key-value pair to be added in the OrderedDict
key : r
value : 1
Resultant Dictionary :OrderedDict([('r', '1'), ('javascript', '4'), ('python', '12')])
Method 2: Using Python's built-in method
Python provides a built-in method to add a key-value pair to the orderedDict using update() method and then move the pair to the beginning of the orderedDict using move_to_end() method.
Program to insert an element at the beginning in OrderedDict
# Python program to insert an element at the beginning in OrderedDict
from collections import OrderedDict
# creating a OrderedDict
myDict = OrderedDict([('javascript', '4'), ('python', '12')])
print("Original dictionary : ", str(myDict))
print("Enter the key-value pair to be added in the OrderedDict")
key = input("key : ")
value = input("value : ")
addedDict = OrderedDict([(key, value)])
# Inserting key-value pair at the beginning of the OrderedDict
myDict.update({key:value})
myDict.move_to_end(key, last = False)
# Printing the Resultant dictionary
print ("Resultant Dictionary : "+str(myDict))
Output:
Original dictionary : OrderedDict([('javascript', '4'), ('python', '12')])
Enter the key-value pair to be added in the OrderedDict
key : r
value : 1
Resultant Dictionary : OrderedDict([('r', '1'), ('javascript', '4'), ('python', '12')])
Python Dictionary Programs »