Python program to change the value of a dictionary if it equals K
Last Updated :
22 Apr, 2023
Given a dictionary, change the value if it is equal to K.
Input : test_dict = {“Gfg”: 4, “is”: 8, “best”: 10, “for”: 8, “geeks”: 19}, K = 8, repl_val = 25
Output : {‘Gfg’: 4, ‘is’: 25, ‘best’: 10, ‘for’: 25, ‘geeks’: 19}
Explanation : All values with 8 converted to 25.
Input : test_dict = {“Gfg”: 6, “is”: 8, “best”: 10, “for”: 8, “geeks”: 19}, K = 6, repl_val = 25
Output : {‘Gfg’: 25, ‘is’: 8, ‘best’: 10, ‘for’: 8, ‘geeks’: 19}
Explanation : All values with 6 converted to 25.
Method #1 : Using a loop
This is a brute way in which this task can be performed. In this, we iterate for all the keys and values of dictionaries, and if found a match, then the required conversion is carried out.
Python3
test_dict = { "Gfg" : 4 , "is" : 8 , "best" : 10 , "for" : 8 , "geeks" : 19 }
print ( "The original dictionary is : " + str (test_dict))
K = 8
repl_val = 20
for key, val in test_dict.items():
if val = = K:
test_dict[key] = repl_val
print ( "The dictionary after values replacement : " + str (test_dict))
|
Output
The original dictionary is : {‘Gfg’: 4, ‘is’: 8, ‘best’: 10, ‘for’: 8, ‘geeks’: 19} The dictionary after values replacement : {‘Gfg’: 4, ‘is’: 20, ‘best’: 10, ‘for’: 20, ‘geeks’: 19}
Time complexity: O(n), where n is the number of items in the dictionary. This is because the code uses a for loop to iterate over all items in the dictionary, and the operations inside the loop take constant time. Hence, the time complexity is linear in the number of items in the dictionary.
Space complexity: O(1), as the code only uses a constant amount of additional space to store the values of K and repl_val.
Method #2 : Using dictionary comprehension
This is a one-liner alternative to solve this problem. In this, we iterate and assign creating a new dictionary rather than doing in-place replacement as in the above method.
Python3
test_dict = { "Gfg" : 4 , "is" : 8 , "best" : 10 , "for" : 8 , "geeks" : 19 }
print ( "The original dictionary is : " + str (test_dict))
K = 8
repl_val = 20
res = {key : repl_val if val = = K else val for key, val in test_dict.items()}
print ( "The dictionary after values replacement : " + str (res))
|
Output
The original dictionary is : {‘Gfg’: 4, ‘is’: 8, ‘best’: 10, ‘for’: 8, ‘geeks’: 19} The dictionary after values replacement : {‘Gfg’: 4, ‘is’: 20, ‘best’: 10, ‘for’: 20, ‘geeks’: 19}
Time complexity: O(n), where n is the number of items in the dictionary. This is because the code uses a dictionary comprehension to iterate over all items in the dictionary, and the operations inside the dictionary comprehension take constant time. Hence, the time complexity is linear in the number of items in the dictionary.
Space complexity: O(n), as the code creates a new dictionary res with the same number of items as the original dictionary. Hence, the space complexity is linear in the number of items in the dictionary.
Method 3 : using the map() function and a lambda function to perform the replacement of values.
steps to implement this approach:
- Initialize the dictionary test_dict
- Initialize the key K and the replacement value repl_val
- Use the map() function to apply a lambda function to each value in the dictionary.
- The lambda function checks if the current value is equal to K, and if so, returns repl_val. Otherwise, it returns the original value.
- Convert the result of map() back to a dictionary using the dict() function.
- Print the original dictionary and the updated dictionary.
Python3
test_dict = { "Gfg" : 4 , "is" : 8 , "best" : 10 , "for" : 8 , "geeks" : 19 }
print ( "The original dictionary is : " + str (test_dict))
K = 8
repl_val = 20
res = dict ( map ( lambda x: (x[ 0 ], repl_val) if x[ 1 ] = = K else (x[ 0 ], x[ 1 ]), test_dict.items()))
print ( "The dictionary after values replacement : " + str (res))
|
Output
The original dictionary is : {'Gfg': 4, 'is': 8, 'best': 10, 'for': 8, 'geeks': 19}
The dictionary after values replacement : {'Gfg': 4, 'is': 20, 'best': 10, 'for': 20, 'geeks': 19}
The time complexity of this approach is O(n), where n is the number of elements in the dictionary.
The space complexity is also O(n), since we need to create a new dictionary to store the updated values.
Share your thoughts in the comments
Please Login to comment...