
- 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 a dictionary using list with none values
Suppose you are given a list but we want to convert it to dictionary. Dictionary elements hold two values are called key value pair, we will use in case of value. The elements of the list become keys and non will remain a placeholder.
With dict
The dict() constructor creates a dictionary in Python. So we will use it to create a dictionary. The fromkeys method is used to create the dictionary elements.
Example
listA = ["Mon","Tue","Wed","Thu","Fri"] print("Given list: \n", listA) res = dict.fromkeys(listA) # New List print("The list of lists:\n",res)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] The list of lists: {'Mon': None, 'Tue': None, 'Wed': None, 'Thu': None, 'Fri': None}
With zip and dict
We can also use the dict constructor with zip method so that each element is converted into a key value pair.
Example
listA = ["Mon","Tue","Wed","Thu","Fri"] print("Given list: \n", listA) res = dict(zip(listA, [None]*len(listA))) # New List print("The list of lists:\n",res)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] The list of lists: {'Mon': None, 'Tue': None, 'Wed': None, 'Thu': None, 'Fri': None}
With dict comprehension
Create a for loop to loop through each element of the list and assign None as key.
Example
listA = ["Mon","Tue","Wed","Thu","Fri"] print("Given list: \n", listA) res = {key: None for key in listA} # New List print("The list of lists:\n",res)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] The list of lists: {'Mon': None, 'Tue': None, 'Wed': None, 'Thu': None, 'Fri': None}
- Related Articles
- Find indices with None values in given list in Python
- Python – Inverse Dictionary Values List
- How to create a dictionary with list comprehension in Python?
- How to create Python dictionary from list of keys and values?
- Python – Create dictionary from the list
- Python Program – Create dictionary from the list
- Combining values from dictionary of list in Python
- Sort Dictionary key and values List in Python
- Python – Limit the values to keys in a Dictionary List
- Explain how a dataframe structure can be created using list of dictionary values in Python?
- Convert key-values list to flat dictionary in Python
- Dictionary creation using list contents in Python
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- How to get a list of all the values from a Python dictionary?
- Counting the frequencies in a list using dictionary in Python

Advertisements