Python | Initialize dictionary with common value
While working with Python, sometimes, we might have a problem in which we require the initialize the static list into a dictionary with a constant value. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using dict()
+ list comprehension
The combination of above functions can be used to perform this particular task. In this, we just convert the elements extracted from list as keys and assign the common value using list comprehension and conversion done by dict().
# Python3 code to demonstrate working of # Initialize dictionary with common value # Using list comprehension + dict() # Initialize list test_list = [ 'gfg' , 'is' , 'best' ] # printing original list print ( "The original list is : " + str (test_list)) # Initialize dictionary with common value # Using list comprehension + dict() res = dict ((sub, 4 ) for sub in test_list) # printing result print ( "The constructed dictionary with common value : " + str (res)) |
Output :
The original list is : ['gfg', 'is', 'best'] The constructed dictionary with common value : {'is': 4, 'gfg': 4, 'best': 4}
Method #2 : Using fromkeys()
The inbuilt function of fromkeys() can also be used to perform this particular task which is made to perform this particular task itself and is more Pythonic way to perform this task.
# Python3 code to demonstrate working of # Initialize dictionary with common value # Using fromkeys() # Initialize list test_list = [ 'gfg' , 'is' , 'best' ] # printing original list print ( "The original list is : " + str (test_list)) # Initialize dictionary with common value # Using fromkeys() res = dict .fromkeys(test_list, 4 ) # printing result print ( "The constructed dictionary with common value : " + str (res)) |
Output :
The original list is : ['gfg', 'is', 'best'] The constructed dictionary with common value : {'is': 4, 'gfg': 4, 'best': 4}
Please Login to comment...