Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Initialize a dictionary with only keys from a list

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




# Python code to initialize a dictionary
# with only keys from a list
 
# List of keys
keyList = ["Paras", "Jain", "Cyware"]
 
# initialize dictionary
d = {}
 
# iterating through the elements of list
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




# Python code to initialize a dictionary
# with only keys from a list
 
# List of Keys
keyList = ["Paras", "Jain", "Cyware"]
 
# Using Dictionary comprehension
myDict = {key: None for key in keyList}
print(myDict)


Output:

{'Paras': None, 'Jain': None, 'Cyware': None}

  Method #3 : Using zip() function 

Python3




# Python code to initialize a dictionary
# with only keys from a list
 
# List of keys
listKeys = ["Paras", "Jain", "Cyware"]
 
# using zip() function to create a dictionary
# with keys and same length None value
dct = dict(zip(listKeys, [None]*len(listKeys)))
 
# print dict
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




# Python code to initialize a dictionary
# with only keys from a list
 
# List of keys
Student = ["Paras", "Jain", "Cyware"]
 
# using fromkeys() method
StudentDict = dict.fromkeys(Student, None)
 
# printing dictionary
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):
    # Initialize an empty dictionary
    d = {}
    # Use the dict.__init__ method to add the keys from the input list to the dictionary
    d.__init__(zip(key_list, [None]*len(key_list)))
    return d
 
 
key_list = ["Paras", "Jain", "Cyware"]
print(create_dict(key_list))  # {'Paras': None, 'Jain': None, 'Cyware': None}
# This code is contributed by Edula Vinay Kumar Reddy


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

RELATED ARTICLES

Most Popular

Recent Comments