Python | Initialize a dictionary with only keys from a list
Given a List, the task is to create a dictionary with only keys by using given list as keys. Let’s see the different methods we can do this task.
Method #1 : By iterating through list
Python3
keyList = [ "Paras" , "Jain" , "Cyware" ]
d = {}
for i in keyList:
d[i] = None
print (d)
|
Output:
{'Cyware': None, 'Paras': None, 'Jain': None}
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Method #2: Using dictionary comprehension
Python3
keyList = [ "Paras" , "Jain" , "Cyware" ]
myDict = {key: None for key in keyList}
print (myDict)
|
Output:
{'Paras': None, 'Jain': None, 'Cyware': None}
Method #3 : Using zip() function
Python3
listKeys = [ "Paras" , "Jain" , "Cyware" ]
dct = dict ( zip (listKeys, [ None ] * len (listKeys)))
print (dct)
|
Output:
{'Cyware': None, 'Paras': None, 'Jain': None}
Time Complexity: O(n), where n is the length of the listKeys list. The zip() function has a linear time complexity, and so does the dict() constructor.
Auxiliary Space: O(n), where n is the length of the listKeys list. We are creating a dictionary with n key-value pairs, where each value is None. The space complexity of the zip() function is also linear in the length of the input lists.
Method #4 : Using fromkeys() method
Python3
Student = [ "Paras" , "Jain" , "Cyware" ]
StudentDict = dict .fromkeys(Student, None )
print (StudentDict)
|
Output:
{'Cyware': None, 'Jain': None, 'Paras': None}
The time complexity of the code is O(n), where n is the number of elements in the list ‘Student’.
The auxiliary space used by the code is O(n), where n is the number of elements in the list ‘Student’.
Method #5: Using the dict.__init__ method:
Here is another approach to creating a dictionary with only keys from a list, using the dict.__init__ method:
Python3
def create_dict(key_list):
d = {}
d.__init__( zip (key_list, [ None ] * len (key_list)))
return d
key_list = [ "Paras" , "Jain" , "Cyware" ]
print (create_dict(key_list))
|
Output
{'Paras': None, 'Jain': None, 'Cyware': None}
This approach uses the dict.__init__ method to initialize the dictionary with the keys from the input list and None as the values.
The time complexity of the create_dict function is O(n), where n is the number of keys in the input list. This is because the function iterates through the input list once, and performs a constant number of operations on each iteration.
The space complexity of the create_dict function is also O(n), because the function creates a new dictionary with n keys. Additionally, the function creates a list of n None values to pass as the values for the keys in the dictionary.
Method#6: Using Recursive method.
This recursive function takes a list of keys as an argument and returns a dictionary with those keys as keys and None as values. It uses Python’s unpacking syntax (**) to merge the dictionary generated for the rest of the keys with the dictionary generated for the first key.
Python3
def initialize_dict(key_list):
if len (key_list) = = 0 :
return {}
else :
key = key_list[ 0 ]
rest_keys = key_list[ 1 :]
return {key: None , * * initialize_dict(rest_keys)}
key_list = [ "Paras" , "Jain" , "Cyware" ]
d = initialize_dict(key_list)
print (d)
|
Output
{'Paras': None, 'Jain': None, 'Cyware': None}
Time complexity: O(n) – The function is called recursively once for each key in the list, so the time complexity is O(n), where n is the number of keys in the list.
Auxiliary Space O(n) – The maximum depth of the call stack is n, since the function is called recursively once for each key in the list, so the space complexity is also O(n). Additionally, we create a dictionary with n keys and n values, so the total space complexity is O(n).
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...