Python | K modulo on each Dictionary Key
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 like K modulo on each key. 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 of K modulo.
Python3
test_dict = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
print ("The original dictionary : " + str (test_dict))
K = 4
for key in test_dict:
test_dict[key] % = 4
print ("The dictionary after mod K each key's value : " + str (test_dict))
|
Output :
The original dictionary : {'is': 4, 'best': 7, 'gfg': 6}
The dictionary after mod K each key's value : {'is': 0, 'best': 3, 'gfg': 2}
Time Complexity: O(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 #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 % K over the dictionary.
Python3
test_dict = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
print ("The original dictionary : " + str (test_dict))
K = 4
test_dict.update((x, y % K) for x, y in test_dict.items())
print ("The dictionary after mod K each key's value : " + str (test_dict))
|
Output :
The original dictionary : {'is': 4, 'best': 7, 'gfg': 6}
The dictionary after mod K each key's value : {'is': 0, 'best': 3, 'gfg': 2}
Method #3 : Using dictionary comprehension
Python3
test_dict = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
print ( "The original dictionary : " + str (test_dict))
K = 4
test_dict = {key: value % K for key, value in test_dict.items()}
print ( "The dictionary after mod K each key's value : " + str (test_dict))
|
Output
The original dictionary : {'gfg': 6, 'is': 4, 'best': 7}
The dictionary after mod K each key's value : {'gfg': 2, 'is': 0, 'best': 3}
# Time complexity : O(n), n is the number of keys in the dictionary.
# Space complexity : O(n), n is the number of keys in the dictionary.
Method #4 : Using operator.mod(),keys() methods
Approach
- Initiate a dictionary to traverse the list of dictionary keys
- Create a new dictionary with key of test_dict and value modulo K as value
- Display new dictionary
Python3
test_dict = { 'gfg' : 6 , 'is' : 4 , 'best' : 7 }
print ( "The original dictionary : " + str (test_dict))
K = 4
import operator
res = dict ()
for i in list (test_dict.keys()):
res[i] = operator.mod(test_dict[i],K)
print ( "The dictionary after mod K each key's value : " + str (res))
|
Output
The original dictionary : {'gfg': 6, 'is': 4, 'best': 7}
The dictionary after mod K each key's value : {'gfg': 2, 'is': 0, 'best': 3}
Time Complexity : O(N) N – length of dictionary keys list
Auxiliary Space : O(N) length of new dictionary
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...