Open In App

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 initialized 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))

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 initialized list
print ("initial list", str(ini_list))
 
# Converting list into dict()
res = dict.fromkeys(ini_list)
 
# printing final result
print ("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  




# Python code to demonstrate converting
# list into dictionary with none values
# using dict comprehension
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized 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))

Output: 
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

 

Method #4: using enumerate()




# Initialize a list
ini_list = [1, 2, 3, 4, 5]
 
# Print the initial list
print("initial list", str(ini_list))
 
# Use a dictionary comprehension and the enumerate() function
# to convert the list into a dictionary with None values
res = {k: None for i, k in enumerate(ini_list)}
 
# Print the final dictionary
print("final dictionary", str(res))
#this code is contributed Vinay Pinjala.

Output
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

Time Complexity:O(N)
Auxiliary Space :O(N)

Method #5: Using a loop and the setdefault() method of the dictionary

Steps:




# Python code to demonstrate converting list
# into dictionary with none values
# using a loop and setdefault()
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print ("initial list", str(ini_list))
 
# Converting list into dictionary using
# a loop and setdefault()
res = {}
for element in ini_list:
    res.setdefault(element, None)
 
# printing final result
print ("final dictionary", str(res))

Output
initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

Time complexity: O(n), where n is the length of the list ini_list.
Auxiliary space: O(n), to store the dictionary with the key-value pairs.

Method #6: Using pop() function and a while loop to create the dictionary with None values

  1. Initialize the input list, ini_list.
  2. Initialize an empty dictionary, res.
  3. Loop over the range of indices of ini_list in ascending order:
    • Use pop(0) to remove the first element from ini_list.
    • Add the removed element as a key to the res dictionary with a value of None.
  4. Print the resulting res dictionary.
     




# initialize the input list
ini_list = [1, 2, 3, 4, 5]
 
# initialize an empty dictionary
res = {}
 
# printing initialized list
print("initial list", str(ini_list))
 
# loop over the range of indices of
# ini_list in ascending order
for i in range(len(ini_list)):
    res[ini_list.pop(0)] = None
 
# printing final result
print("final dictionary", str(res))

Output
{1: None, 2: None, 3: None, 4: None, 5: None}

Time complexity: O(n), where n is the length of the input list. 
Space complexity: O(n), where n is the length of the input list. 

Method #7: Using map and lambda functions:




# Python code to demonstrate
# converting list into dictionary with none values
# using map() and lambda functions
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print("initial list:", ini_list)
 
# Converting list into dictionary using map() and lambda functions
res = dict(map(lambda x: (x, None), ini_list))
 
# printing final result
print("final dictionary:", res)

Output
initial list: [1, 2, 3, 4, 5]
final dictionary: {1: None, 2: None, 3: None, 4: None, 5: None}

Time complexity: O(n), where n is the length of the input list. 
Space complexity: O(n), where n is the length of the input list. 


Article Tags :