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)) |
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)) |
The original list is : ['gfg', 'is', 'best']
The constructed dictionary with common value : {'is': 4, 'gfg': 4, 'best': 4}
Recommended Posts:
- Python | Initialize dictionary with None values
- Python | Initialize dictionary with multiple keys
- Python | Initialize a dictionary with only keys from a list
- Python | Initialize common value to keys
- Python | Combine two dictionary adding values for common keys
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Initialize Matrix in Python
- Python | Which is faster to initialize lists?
- Python | Initialize tuples with parameters
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Python | Initialize list with empty dictionaries
- Python | Ways to initialize list with alphabets
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.



