Q:

Python program to insert an element at the beginning in OrderedDict

belongs to collection: Python Dictionary Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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')])

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to sort dictionary key and values l... >>
<< Python program to convert key-value list to flat d...