Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Even values update in dictionary

Python | Even values update in dictionary

Sometimes, while working with dictionaries, we might come across a problem in which we require to perform a particular operation on even value of keys. This type of problem can occur in web development domain. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loop This is the naive method in which this task can be performed. In this we simply run a loop to traverse each key and check for even in dictionary and perform the desired operation. 

Python3




# Python3 code to demonstrate working of
# Even values update in dictionary
# Using loop
 
# Initialize dictionary
test_dict = {'gfg' : 6, 'is' : 4, 'best' : 7}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Using loop
# Even values update in dictionary
for key in test_dict:
    if test_dict[key] % 2 == 0:
        test_dict[key] *= 3
 
# printing result
print("The dictionary after triple even key's value : " + str(test_dict))


Output : 

The original dictionary : {'best': 7, 'is': 4, 'gfg': 6}
The dictionary after triple even key's value : {'best': 7, 'is': 12, 'gfg': 18}

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #2 : Using update() + dictionary comprehension An alternate one-liner to perform this task, the combination of above functions can be used to perform this particular task. The update function is used to perform the necessary operation over the dictionary. 

Python3




# Python3 code to demonstrate working of
# Even values update in dictionary
# Using update() + dictionary comprehension
 
# Initialize dictionary
test_dict = {'gfg' : 6, 'is' : 4, 'best' : 7}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Using update() + dictionary comprehension
# Even values update in dictionary
test_dict.update((x, y * 3) for x, y in test_dict.items() if y % 2 == 0)
 
# printing result
print("The dictionary after triple even key's value : " + str(test_dict))


Output : 

The original dictionary : {'best': 7, 'is': 4, 'gfg': 6}
The dictionary after triple even key's value : {'best': 7, 'is': 12, 'gfg': 18}

Time Complexity: O(n*n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method 3: Using the map() function along with a lambda function to check for even values and triple them. 

Step-by-step approach:

  • Initialize dictionary
  • Print the original dictionary
  • Use map() function along with a lambda function to check for even values and triple them
  • Convert the output of map() back to a dictionary using the dict() constructor
  • Update the original dictionary with the new dictionary containing updated values
  • Print the updated dictionary

Below is the implementation of the above approach:

Python




# Python3 code to demonstrate working of
# Even values update in dictionary
# Using map() and lambda
 
# Initialize dictionary
test_dict = {'gfg': 6, 'is': 4, 'best': 7}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Using map() and lambda function
# Even values update in dictionary
new_dict = dict(map(lambda x: (x[0], x[1]*3) if x[1]%2==0 else (x[0], x[1]), test_dict.items()))
 
# Update original dictionary with new dictionary
test_dict.update(new_dict)
 
# printing result
print("The dictionary after triple even key's value : " + str(test_dict))


Output

The original dictionary : {'is': 4, 'gfg': 6, 'best': 7}
The dictionary after triple even key's value : {'is': 12, 'gfg': 18, 'best': 7}

Time complexity: O(n), where n is the number of elements in the dictionary
Auxiliary space: O(n), as we are creating a new dictionary to store updated values.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments