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


Dictionary is one of the data structure available in python to store any datatype of data in the format of key and value pair. It is denoted by {} curly braces. The key in the dictionary is unique and the values in the dictionary can be repeated. The key and values pairs are separated by using the semi colon ‘:’. It is mutable i.e. once the dictionary created, we can modify the data.

Dictionaries are versatile and widely used in Python for mapping and storing data where quick access to values based on keys is required.

The dictionary is unordered and can be nested. It allows the user to access the values by using the appropriate key. There are many functions that supports dictionary to modify and access values of the particular key.

In this article we are going to change the value of a dictionary if it equals to K by using various techniques. Let’s go through each one.

Using a ‘for’ loop

We all know that for loop is used to iterate all the values in a defined data structure. Here we are using for loop to iterate the items of the dictionary and updates the value when it is equal to K.

Example

Here in this example we are trying to iterate the dictionary items using for loop and check if any value is equal to value of K then update the key with the new value.

def change_value_if_equals_K(dictionary, K, new_value):
   for key, value in dictionary.items():
      if value == K:
         dictionary[key] = new_value
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print("The dictionary before update:",my_dict)
K = 2 
new_value = 5  
change_value_if_equals_K(my_dict, K, new_value)
print("The updated dictionary:",my_dict)  

Output

The dictionary before update: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
The updated dictionary: {'a': 1, 'b': 5, 'c': 3, 'd': 4}

Using a dictionary comprehension

Dictionary comprehension is the simple and easy mechanism to create the new dictionary by performing the filtering, mapping and conditional logics on the existing dictionary items within no time.

Example

Here we are creating a new dictionary by passing the conditional logic for checking if any value of the dictionary is equal is to K=2 then update the key with the new defined value 5.

def change_value_if_equals_K(dictionary, K, new_value):
   new_dict = {key: new_value if value == K else value for key, value in dictionary.items()}
   return new_dict
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print("The dictionary before update:",my_dict)
K = 2  
new_value = 5  
updated_dict = change_value_if_equals_K(my_dict, K, new_value)
print("The updated dictionary:",my_dict)  

Output

The dictionary before update: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
The updated dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Using map() function

The map() function in Python applies a given function to each element of an iterable and returns an iterator of the results.

While map() is typically used with functions, it can also be used with lambda functions to change the value of a dictionary if it equals a specific K value .

Example

In this example we are using the dictionary comprehension to check if any dictionary value is equal to K=2 and if it matches it is replaced with the new value. The map() function is then used with a lambda function in the form lambda item: (item[0], new_value) if item[1] == K else item to iterate over the items of the dictionary. The lambda function checks if the value (item[1]) equals K. If it does, it returns a tuple with the key (item[0]) and the new value (new_value). Otherwise, it returns the original key-value pair (item).

def change_dict_value(dictionary, K, new_value):
   return {key: new_value if value == K else value for key, value in dictionary.items()}
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
print("The dictionary before update:",my_dict)
K = 2 
new_value = '5'  
new_dict = dict(map(lambda item: (item[0], new_value) if item[1] == K else item, my_dict.items()))
print("The updated dictionary:",new_dict)  

Output

The dictionary before update: {'a': 1, 'b': 2, 'c': 3, 'd': 2}
The updated dictionary: {'a': 1, 'b': '5', 'c': 3, 'd': '5'}

Updated on: 19-Oct-2023

30 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements