Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Add item after given Key in dictionary

  • Last Updated : 01 Aug, 2020

Given a dictionary and a Key, add new item after a particular key in dictionary.

Input : test_dict = {“Gfg” : 3, “is” : 5, “for” : 8, “Geeks” : 10}, K = “is”, add_item = {“good” : 19}
Output : {‘Gfg’: 3, ‘is’: 5, ‘good’: 19, ‘for’: 8, ‘Geeks’: 10}
Explanation : Item added after desired key in dictionary.

Input : test_dict = {“Gfg” : 3, “is” : 5, “for” : 8, “Geeks” : 10}, K = “for”, add_item = {“good” : 19}
Output : {‘Gfg’: 3, ‘is’: 5, ‘for’: 8, ‘good’: 19, ‘Geeks’: 10}
Explanation : Item added after desired key in dictionary.

Method : Using loop + update()

In this we iterate for all the keys, and when target key is encountered, the iteration is haulted and dictionary is updated with required key. Then iteration is resumed.

Python3




# Python3 code to demonstrate working of 
# Dictionary Keys whose Values summation equals K 
# Using loop + update()
  
# initializing dictionary
test_dict = {"Gfg" : 3, "is" : 5, "for" : 8, "Geeks" : 10}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing K 
K = "is"
  
# initializing dictionary to be added 
add_item = {"best" : 19}
  
# using dictionary comprehension 
res = dict()
for key in test_dict:
    res[key] = test_dict[key]
      
    # modify after adding K key
    if key == K:
        res.update(add_item)
  
# printing result 
print("Modified dictionary : " + str(res)) 
Output
The original dictionary is : {'Gfg': 3, 'is': 5, 'for': 8, 'Geeks': 10}
Modified dictionary : {'Gfg': 3, 'is': 5, 'best': 19, 'for': 8, 'Geeks': 10}

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!