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
Python3
# Python code to demonstrate# converting list into dictionary with none values# using zip() and dictionary# initializing listini_list = [1, 2, 3, 4, 5]# printing initialized listprint ("initial list", str(ini_list))# Converting list into dictionary using zip() and dictionaryres = dict(zip(ini_list, [None]*len(ini_list)))# printing final resultprint ("final dictionary", str(res)) |
Output:
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}
Method #2: Using dict
Python3
# Python code to demonstrate converting# list into dictionary with none values# using dict()# initializing listini_list = [1, 2, 3, 4, 5]# printing initialized listprint ("initial list", str(ini_list))# Converting list into dict()res = dict.fromkeys(ini_list)# printing final resultprint ("final dictionary", str(res)) |
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
Python3
# Python code to demonstrate converting# list into dictionary with none values# using dict comprehension# initializing listini_list = [1, 2, 3, 4, 5]# printing initialized listprint ("initial list", str(ini_list))# Converting list into dict()res = {key: None for key in ini_list}# printing final resultprint ("final dictionary", str(res)) |
Output:
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}




.png)