
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Create dictionary from the list
When it is required to create dictionary from a list, the ‘fromkeys’ method in the ‘dict’ method is used.
Example
Below is a demonstration of the same −
my_list = ['Hi', 'Will', 'how', 'Python', 'cool'] print("The list is ") print(my_list) my_dict = dict.fromkeys(my_list, "my_value") print(my_dict)
Output
The list is ['Hi', 'Will', 'how', 'Python', 'cool'] {'Hi': 'my_value', 'Will': 'my_value', 'how': 'my_value', 'Python': 'my_value', 'cool': 'my_value'}
Explanation
A list of strings is defined and is displayed on the console.
The ‘fromkeys’ method present in ‘dict’ is used to convert the elements of the list to dictionary keys.
The value for every key is specified here itself.
This is assigned to a variable.
It is displayed as output on the console.
- Related Articles
- Python Program – Create dictionary from the list
- How to create Python dictionary from list of keys and values?
- How to create Python dictionary from the value of another dictionary?
- Python - Create a dictionary using list with none values
- Combining values from dictionary of list in Python
- How to create Python dictionary from JSON input?
- How to create a dictionary with list comprehension in Python?
- How to create a Python dictionary from text file?
- In Python how to create dictionary from two lists?
- Python program to create a dictionary from a string
- Create a Series from a List, Numpy Array, and Dictionary in Pandas
- How to create a Pandas series from a python dictionary?
- Python – Inverse Dictionary Values List
- How to get a list of all the keys from a Python dictionary?
- How to get a list of all the values from a Python dictionary?

Advertisements