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

A dictionary is one of Python's most versatile data structures, storing data in key-value pairs using {} curly braces. Dictionary keys are unique, values can be repeated, and the structure is mutable, allowing modifications after creation.

In this article, we'll explore different techniques to change dictionary values that equal a specific value K. Let's examine each approach with practical examples.

Using a For Loop

The for loop iterates through dictionary items and updates values when they match our target value K ?

Example

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("Original dictionary:", my_dict)

K = 2 
new_value = 5  
change_value_if_equals_K(my_dict, K, new_value)
print("Updated dictionary:", my_dict)
Original dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Updated dictionary: {'a': 1, 'b': 5, 'c': 3, 'd': 4}

Note: This method modifies the original dictionary in-place.

Using Dictionary Comprehension

Dictionary comprehension provides a concise way to create a new dictionary with conditional logic applied to each key-value pair ?

Example

def change_value_if_equals_K(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': 4}
print("Original dictionary:", my_dict)

K = 2  
new_value = 5  
updated_dict = change_value_if_equals_K(my_dict, K, new_value)
print("Updated dictionary:", updated_dict)
print("Original unchanged:", my_dict)
Original dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Updated dictionary: {'a': 1, 'b': 5, 'c': 3, 'd': 4}
Original unchanged: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Note: This method creates a new dictionary, leaving the original unchanged.

Using map() Function

The map() function applies a lambda function to each dictionary item, transforming key-value pairs based on our condition ?

Example

my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 2}
print("Original dictionary:", my_dict)

K = 2 
new_value = 5  

# Using map with lambda to transform items
updated_dict = dict(map(lambda item: (item[0], new_value) if item[1] == K else item, my_dict.items()))
print("Updated dictionary:", updated_dict)
Original dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 2}
Updated dictionary: {'a': 1, 'b': 5, 'c': 3, 'd': 5}

Comparison of Methods

Method Modifies Original Memory Usage Best For
For Loop Yes Low In-place modifications
Dictionary Comprehension No Medium Readable, functional style
map() Function No Medium Functional programming approach

Conclusion

Use the for loop approach when you need to modify the original dictionary in-place. Dictionary comprehension offers the most readable solution for creating new dictionaries with transformed values. The map() function provides a functional programming approach suitable for complex transformations.

Updated on: 2026-03-27T15:48:23+05:30

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements