Python | Initialize common value to keys
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[ 'Beginner' ] # 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( 'Beginner' , 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 [âBeginnerâ, âforâ, âBeginnerâ]. 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 âBeginnerâ and the value is 4.
- Now, accesses the key âBeginnerâ 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 [ 'Beginner' , 'for' , 'Beginner' ]: test_dict[key] = common_value # Access a key that we set a common value for res_demo = test_dict[ 'Beginner' ] # 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.
Contact Us