Skip to content
Related Articles

Related Articles

Improve Article

Python | Initialize common value to keys

  • Last Updated : 26 Aug, 2019

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 in 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 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))
Output :
The original dictionary is : {}
The value of key is :  4

 



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 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))
Output :
The original dictionary is : {}
The value of key is :  4

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :