Python - Custom dictionary initialization in list
Last Updated :
13 Apr, 2023
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))
OutputThe 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))
OutputThe 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
OutputThe 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)
Similar Reads
Python - Initialize dictionary with custom value list 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.
4 min read
Python | Dictionary initialization with common dictionary Sometimes, while working with dictionaries, we might have an utility in which we need to initialize a dictionary with records values, so that they can be altered later. This kind of application can occur in cases of memoizations in general or competitive programming. Letâs discuss certain way in whi
7 min read
Python | Initializing dictionary with list index-values While working with Python we might need to perform tasks in which we need to assign a dictionary with list values as dictionary values and index as dictionary keys. This type of problem is quite common in cases we need to perform data-type conversion. Let's discuss certain ways in which this task ca
4 min read
Python | Initializing dictionary with list index values While working with dictionaries, we might come across a problem in which we require to attach each value in list with it's index, to be used afterwards to solve question. This technique is usually very useful in competitive programming domain. Let's discuss certain ways in which this task can be per
5 min read
How to Initialize Dictionary in Python In Python dictionary, each key is unique and we can store different data types as values. The simplest and most efficient method to initialize a dictionary is using curly braces {}. This is the easiest and most common way to create a dictionary. We can use curly braces '{} ' and separate the keys an
2 min read
Python - Incremental value initialization in Dictionary The interconversion between the datatypes is very popular and hence many articles have been written to demonstrate different kind of problems with their solutions. This article deals with yet another similar type problem of converting a list to dictionary, with values as the index incremental with K
5 min read
Python | Initialize list with empty dictionaries While working with Python, we can have a problem in which we need to initialize a list of a particular size with empty 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 {} + "*" operator Thi
5 min read
Python | Initialize dictionary with common value While working with Python, sometimes, we might have a problem in which we require the initialize the static list into a dictionary with a constant value. Let's discuss certain ways in which this task can be performed. Method #1: Using dict() + list comprehension The combination of above functions ca
5 min read
Python - Custom order dictionary Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the custom ordering of keys of dictionary. This is quite popular problem, with the advent of newer version of Python, where keys are ordered in Dictionaries, there might be requirement to reorder dic
3 min read
Dictionary keys as a list in Python In Python, we will encounter some situations where we need to extract the keys from a dictionary as a list. In this article, we will explore various easy and efficient methods to achieve this.Using list() The simplest and most efficient way to convert dictionary keys to lists is by using a built-in
2 min read