Open In App

Python program to change the value of a dictionary if it equals K

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Change value if value equals K in dictionary
# Using loop
 
# initializing dictionary
test_dict = {"Gfg": 4, "is": 8, "best": 10, "for": 8, "geeks": 19}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K 
K = 8
 
# initializing repl_val
repl_val = 20
 
# iterating dictionary
for key, val in test_dict.items():
     
    # checking for required value
    if val == K:
        test_dict[key] = repl_val
 
# printing result
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




# Python3 code to demonstrate working of
# Change value if value equals K in dictionary
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {"Gfg": 4, "is": 8, "best": 10, "for": 8, "geeks": 19}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K 
K = 8
 
# initializing repl_val
repl_val = 20
 
# one-liner to solve for dictionary
res = {key : repl_val if val == K else val for key, val in test_dict.items()}
 
# printing result
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:

  1. Initialize the dictionary test_dict
  2. Initialize the key K and the replacement value repl_val
  3. Use the map() function to apply a lambda function to each value in the dictionary.
  4. The lambda function checks if the current value is equal to K, and if so, returns repl_val. Otherwise, it returns the original value.
  5. Convert the result of map() back to a dictionary using the dict() function.
  6. Print the original dictionary and the updated dictionary.

Python3




# initializing dictionary
test_dict = {"Gfg": 4, "is": 8, "best": 10, "for": 8, "geeks": 19}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K 
K = 8
 
# initializing repl_val
repl_val = 20
 
# using map() and lambda to replace values
res = dict(map(lambda x: (x[0], repl_val) if x[1] == K else (x[0], x[1]), test_dict.items()))
 
# printing result
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads