Python | Create a dictionary using list with none values
Sometimes you might need to convert a list to dict object for some better and fast operation. Let’s see how to convert a list into a dictionary of none values. Here we will find three methods of doing this.
Method #1: Using zip()and dict
# Python code to demonstrate # converting list into dictionary with none values # using zip() and dictionary # initializing list ini_list = [1, 2, 3, 4, 5] # printing intialized list print ("initial list", str(ini_list)) # Converting list into dictionary using zip() and dictionary res = dict(zip(ini_list, [None]*len(ini_list))) # printing final result print ("final dictionary", str(res)) |
chevron_right
filter_none
Output:
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}
Method #2: Using dict
# Python code to demonstrate converting # list into dictionary with none values # using dict() # initializing list ini_list = [1, 2, 3, 4, 5] # printing intialized list print ("initial list", str(ini_list)) # Converting list into dict() res = dict.fromkeys(ini_list) # printing final result print ("final dictionary", str(res)) |
chevron_right
filter_none
Output:
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}
Method #3: Using dict comprehension
# Python code to demonstrate converting # list into dictionary with none values # using dict comprehension # initializing list ini_list = [1, 2, 3, 4, 5] # printing intialized list print ("initial list", str(ini_list)) # Converting list into dict() res = {key: None for key in ini_list} # printing final result print ("final dictionary", str(res)) |
chevron_right
filter_none
Output:
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}
Recommended Posts:
- Python | Combining values from dictionary of list
- Python | Summation of dictionary list values
- Python | Get Unique values from list of dictionary
- Python | Grouping list values into dictionary
- Python | Initializing dictionary with list index-values
- Python | Categorize tuple values into dictionary value list
- Python | Initializing dictionary with list index values
- Python | Element Occurrence in dictionary of list values
- Python | Filter dictionary key based on the values in selective list
- Python program to create a dictionary from a string
- Python | Ways to create a dictionary of Lists
- Python dictionary | values()
- Python | Check for None values in given dictionary
- Python | Sum values for each key in nested dictionary
- Python | N largest values in dictionary
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.



