Open In App

Python – Initialize dictionary with custom value list

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In python one usually comes across situations in which one has to use dictionary for storing the lists. But in those cases, one usually checks for first element and then creates a list corresponding to key when it comes. But its always wanted a method to initialize the dict. keys with a custom list. Let’s discuss certain ways to achieve this particular task. 

Method #1: Using Dictionary comprehension This is most sought of method to do this initialization. In this method, we create the no. of keys we require and then initialize the customlist as we keep on creating the keys, so as to facilitate the append operation afterwards without an error. 

Python3




# Python3 code to demonstrate
# Custom list dictionary initialization
# using dictionary comprehension
 
# initialize custom list
cus_list = [4, 6]
 
# using dictionary comprehension to construct
new_dict = {new_list: cus_list for new_list in range(4)}
     
# printing result
print ("New dictionary with custom list as keys : " + str(new_dict))


Output : 

New dictionary with custom list as keys : {0: [4, 6], 1: [4, 6], 2: [4, 6], 3: [4, 6]}

Time complexity: O(n), where n is the value of range(4), which is 4. The loop runs 4 times, and each iteration takes constant time.
Auxiliary space: O(n), where n is the value of range(4). The program creates a dictionary with 4 key-value pairs, each of which contains a reference to the same custom list object. Since the custom list object is mutable, each reference takes up space in memory.

Method #2: Using fromkeys() fromkeys() can be used to perform this by specifying the additional custom list as argument and the range of elements that need to be the key of dictionary being made. 

Step-by-step approach: 

  • Initialization custom list 
  • Using fromkeys() to construct
  • Printing result

Python3




# Python3 code to demonstrate
# Custom list dictionary initialization
# using fromkeys()
 
# initialization custom list
cus_list = [4, 6]
 
# using fromkeys() to construct
new_dict = dict.fromkeys(range(4), cus_list)
     
# printing result
print ("New dictionary with custom list as keys : " + str(new_dict))


Output : 

New dictionary with custom list as keys : {0: [4, 6], 1: [4, 6], 2: [4, 6], 3: [4, 6]}

Time complexity: O(n), where n is the length of the range provided to the fromkeys() method.

Auxiliary space: O(1), because the size of the dictionary does not depend on the size of the custom list provided as value. 

Method 3-  using a loop in Python . we use a loop to iterate over the range of keys and setting each key-value pair in the dictionary.

Python3




cus_list = [4, 6]
 
new_dict = {}
for key in range(4):
    new_dict[key] = cus_list
 
print("New dictionary with custom list as keys : " + str(new_dict))


Output

New dictionary with custom list as keys : {0: [4, 6], 1: [4, 6], 2: [4, 6], 3: [4, 6]}

Time complexity: The time complexity of this program is O(n), where n is the range of keys.

Auxiliary space: The auxiliary space complexity of this program is O(1) because it only uses a constant amount of additional memory to create an empty dictionary and a custom list.

Method #4: Using dictionary comprehension with enumerate()

 we uses dictionary comprehension that creates a new dictionary using the enumerate() built-in function to generate key-value pairs for the dictionary.

Python3




cus_list = [4, 6]
new_dict = {key: cus_list for key, value in enumerate(range(4))}
print("New dictionary with custom list as keys: " + str(new_dict))


Output

New dictionary with custom list as keys: {0: [4, 6], 1: [4, 6], 2: [4, 6], 3: [4, 6]}

The time complexity of the above code is O(n), where n is the number of elements in the range(4), which is 4. 

The auxiliary space complexity of the code is O(1), because it only creates a new dictionary object and does not create any additional data structures or variables that depend on the size of the input.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads