Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Perform operation on each key dictionary

Python | Perform operation on each key dictionary

Sometimes, while working with dictionaries, we might come across a problem in which we require to perform a particular operation on each 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 in dictionary and perform the desired operation. 

Python3




# Python3 code to demonstrate working of
# Perform operation on each key 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
# Perform operation on each key dictionary
for key in test_dict:    
    test_dict[key] *= 3
 
# printing result
print("The dictionary after triple each key's value : " + str(test_dict))


Output

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

Time Complexity: O(n), where n is the number of keys in the dictionary.

Space Complexity: 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
# Perform operation on each key 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
# Perform operation on each key dictionary
test_dict.update((x, y * 3) for x, y in test_dict.items())
 
# printing result
print("The dictionary after triple each key's value : " + str(test_dict))


Output

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

Method #3 : Using map() + lambda function
This method uses the built-in function map() and lambda function to perform the desired operation on each key of the dictionary.

Python3




# Python3 code to demonstrate working of
# Perform operation on each key dictionary
# Using map() + lambda function
 
# Initialize dictionary
test_dict = {'gfg' : 6, 'is' : 4, 'best' : 7}
 
# printing original dictionary
print("The original dictionary : ", test_dict)
 
# Using map() + lambda function
# Perform operation on each key dictionary
test_dict = dict(zip(test_dict.keys(), map(lambda x: x*3, test_dict.values())))
 
# printing result
print("The dictionary after triple each key's value : ", test_dict)
#This code is contributed by Edula Vinay Kumar Reddy


Output

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

This method utilizes the map() function along with a lambda function to perform the desired operation on each value of the dictionary. The map() function applies the lambda function to each value, and the result is passed to the dict() constructor which creates a new dictionary.

The time complexity of this method is O(n) where n is the number of keys in the dictionary. Auxiliary Space is O(n) as well, as we are creating a new dictionary.

Method #4: Using the dict() constructor and a generator expression

Uses the dict() constructor to create a new dictionary from a generator expression that iterates through the items of the original dictionary and multiplies the values by 3.

  • Create a generator expression that iterates through the items of the test_dict dictionary, multiplies the values by 3, and returns tuples with the same keys and updated values.
  • Pass the generator expression to the dict() constructor to create a new dictionary with the same keys and updated values as the original dictionary.
  • Assign the new dictionary to the variable test_dict.
  • Print the updated dictionary using the print() function.

Python3




# Initialize dictionary
test_dict = {'gfg' : 6, 'is' : 4, 'best' : 7}
 
# printing original dictionary
print("The original dictionary : ", test_dict)
 
# Using dict() constructor and generator expression
# Perform operation on each key dictionary
test_dict = dict((k, v*3) for k, v in test_dict.items())
 
# printing result
print("The dictionary after triple each key's value : ", test_dict)


Output

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

Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n), where n is the number of items in the dictionary, since a new dictionary is created with the same number of items as the original dictionary.

RELATED ARTICLES

Most Popular

Recent Comments