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().



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original list is : ['gfg', 'is', 'best']
The constructed dictionary with common value : {'is': 4, 'gfg': 4, 'best': 4}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.