Python – Add item after given Key in dictionary
Last Updated :
25 Apr, 2023
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 1: Using loop + update()
In this we iterate for all the keys, and when target key is encountered, the iteration is halted and dictionary is updated with required key. Then iteration is resumed.
Python3
test_dict = { "Gfg" : 3 , "is" : 5 , "for" : 8 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
K = "is"
add_item = { "best" : 19 }
res = dict ()
for key in test_dict:
res[key] = test_dict[key]
if key = = K:
res.update(add_item)
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}
Time Complexity: O(n), where n is the values in dictionary
Auxiliary Space: O(n), where n is the size of dictionary
Method 2: Using keys(),values(),items(),insert(),index() methods
Approach
- Access the keys and values of test_dict using keys(),values()methods
- Insert the new key into keys list and value into values list after K using insert(),index() method
- Now create a new dictionary using keys list and values list by for loop
- Display the new dictionary
Python3
test_dict = { "Gfg" : 3 , "is" : 5 , "for" : 8 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
K = "is"
add_item = { "best" : 19 }
res = dict ()
a = list (add_item.items())
x = list (test_dict.keys())
y = list (test_dict.values())
x.insert(x.index(K) + 1 ,a[ 0 ][ 0 ])
y.insert(x.index(K) + 1 ,a[ 0 ][ 1 ])
for i in range ( 0 , len (x)):
res[x[i]] = y[i]
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}
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 3: Using dictionary comprehension and the get() method
- Initialize the dictionary test_dict and the key K with their respective values.
- Initialize the dictionary add_item with the key-value pair that needs to be added to the test_dict.
- Calculate the sum of all values in the dictionary using the sum() method.
- Check if the sum of all values is equal to K or not.
- If the sum is equal to K, return the original dictionary as the result.
- If the sum is not equal to K, add the new key-value pair to the dictionary and return the modified dictionary as the result.
Python3
test_dict = { "Gfg" : 3 , "is" : 5 , "for" : 8 , "Geeks" : 10 }
print ( "The original dictionary is : " + str (test_dict))
K = 18
add_item = { "best" : 19 }
sum_values = sum (test_dict.values())
if sum_values = = K:
res = test_dict
else :
test_dict.update(add_item)
res = test_dict
print ( "Modified dictionary : " + str (res))
|
Output
The original dictionary is : {'Gfg': 3, 'is': 5, 'for': 8, 'Geeks': 10}
Modified dictionary : {'Gfg': 3, 'is': 5, 'for': 8, 'Geeks': 10, 'best': 19}
Time complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary space: O(1), as we are not using any extra space for computation.
Please Login to comment...