Sometimes, while working with Python, we can be confronted with an issue in which we need to assign each key of dictionary with a common value. This type of problem is not occasional but can occur many times while programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using defaultdict() + lambda The defaultdict can be initialized using a function which by default assigns each new key with the common key. This is most recommended way to perform this task.
Python3
# Python3 code to demonstrate working of # Initialize common value to keys # Using defaultdict() from collections import defaultdict # Initialize dictionary test_dict = dict () # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Initialize common value to keys # Using defaultdict() res = defaultdict( lambda : 4 , test_dict) res_demo = res[ 'Geeks' ] # printing result print ("The value of key is : " + str (res_demo)) |
The original dictionary is : {} The value of key is : 4
Time Complexity: O(1)
Space Complexity: O(n) -> where n is the number of elements in the dictionary
Method #2 : Using get() + default value This method is just a display hack to perform this task. It doesn’t create the actual list, but just prints the default value passed to get function and hence the result.
Python3
# Python3 code to demonstrate working of # Initialize common value to keys # Using get() + default value # Initialize dictionary test_dict = dict () # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Initialize common value to keys # Using get() + default value res_demo = test_dict.get( 'Geeks' , 4 ) # printing result print ("The value of key is : " + str (res_demo)) |
The original dictionary is : {} The value of key is : 4
Time Complexity: O(1)
Space Complexity: O(n) -> where n is the number of elements in the dictionary
Method #3: Using a loop to assign the common value to keys
Step-by-step approach to understanding the code you provided:
- Initializes an empty dictionary named ‘test_dict‘.
- Print the original dictionary using the ‘print()’ function and converts the dictionary to a string using the ‘str()’ function.
- Initializes a variable named ‘common_value’ to 4.
- Iterate a for loop that iterates over the list [‘Geeks’, ‘for’, ‘Geeks’]. Inside the for loop, the current key in the iteration is used to assign the ‘common_value’ to the key in the ‘test_dict’ dictionary using the key-value assignment syntax.
- After the for loop finishes, the dictionary ‘test_dict’ will have three key-value pairs, where each key is ‘Geeks’ and the value is 4.
- Now, accesses the key ‘Geeks’ in the dictionary using the indexing operator, and assigns the resulting value to the variable ‘res_demo’.
- Print the value of ‘res_demo’ using the ‘print()’ function and converts the value to a string using the ‘str()’ function.
Below is the implementation of the above approach:
Python3
# Initialize dictionary test_dict = {} # printing original dictionary print ( "The original dictionary is: " + str (test_dict)) # Initialize common value to keys using a loop common_value = 4 for key in [ 'Geeks' , 'for' , 'Geeks' ]: test_dict[key] = common_value # Access a key that we set a common value for res_demo = test_dict[ 'Geeks' ] # printing result print ( "The value of key is: " + str (res_demo)) |
The original dictionary is: {} The value of key is: 4
This method will have a time complexity of O(n), where n is the number of keys in the dictionar= because we need to iterate over each key to assign the common value.
The space complexity is also O(n), because we are storing n key-value pairs in the dictionary.