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
# 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)) |
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
# Python3 code to demonstrate working of # Dictionary Keys whose Values summation equals K # 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 } 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] # printing result print ( "Modified dictionary : " + str (res)) |
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
# Python3 code to demonstrate working of # Dictionary Keys whose Values summation equals K # Using dictionary comprehension and get() # 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 = 18 # initializing dictionary to be added add_item = { "best" : 19 } # calculate the sum of all values in the dictionary sum_values = sum (test_dict.values()) # check if the sum of all values is equal to K or not if sum_values = = K: res = test_dict else : # add the new key-value pair to the dictionary test_dict.update(add_item) # return the modified dictionary res = test_dict # printing result print ( "Modified dictionary : " + str (res)) |
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.