Open In App

Python – Custom dictionary initialization in list

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

While working with Python, we can have a problem in which we need to initialize a list of a particular size with custom dictionaries. This task has it’s utility in web development to store records. Let’s discuss certain ways in which this task can be performed. 
Method #1 : Using {dict} + “*” operator This task can be performed using the “*” operator. We can create a list containing single custom dictionary and then multiply it by Number that is size of list. The drawback is that similar reference dictionaries will be made which will point to similar memory location. 

Python3




# Python3 code to demonstrate working of
# Custom dictionary initialization in list
# using {dict} + "*" operator
 
# Initialize dict
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# Custom dictionary initialization in list
# using {dict} + "*" operator
res = [test_dict] * 6
 
print("The list of custom dictionaries is : " + str(res))


Output

The list of custom dictionaries is : [{'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}]

Time Complexity: O(n), where n is the length of the dictionary 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res dictionary

  Method #2 : Using {dict} + list comprehension This is perhaps the better and correct way to perform this task. We initialize the each index of list with dictionary, this way, we have independently referring dictionaries and don’t point to single reference. 

Python3




# Python3 code to demonstrate working of
# Custom dictionary initialization in list
# using {dict} + list comprehension
 
# Initialize dict
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# Custom dictionary initialization in list
# using {dict} + list comprehension
res = [test_dict for sub in range(6)]
 
print("The list of custom dictionaries is : " + str(res))


Output

The list of custom dictionaries is : [{'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3: Using map() and dict()
This method uses the built-in map() function to apply the dict() constructor to each element in a range of the desired size.

Python3




# Python3 code to demonstrate working of
# Custom dictionary initialization in list
# using map() and dict()
 
# Initialize dict
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3}
 
# Custom dictionary initialization in list
# using map() and dict()
res = list(map(lambda x: dict(test_dict), range(6)))
 
print("The list of custom dictionaries is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The list of custom dictionaries is : [{'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}, {'gfg': 1, 'is': 2, 'best': 3}]

Time complexity: O(n)
Auxiliary Space: O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads