Open In App

Python | Initialize list with empty dictionaries

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 This task can be performed using the “*” operator. We can create a list containing single empty 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
# Initialize list with empty dictionaries
# using {} + "*" operator
 
# Initialize list with empty dictionaries
# using {} + "*" operator
res = [{}] * 6
 
print("The list of empty dictionaries is : " + str(res))


Output : 

The list of empty dictionaries is : [{}, {}, {}, {}, {}, {}]

The time complexity of this code is O(1) as it only involves creating a list of empty dictionaries of a fixed size, which does not depend on the input size.

The auxiliary space complexity of this code is O(n), where n is the size of the list.

  Method #2 : Using {} + 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
# Initialize list with empty dictionaries
# using {} + list comprehension
 
# Initialize list with empty dictionaries
# using {} + "*" operator
res = [{} for sub in range(6)]
 
print("The list of empty dictionaries is : " + str(res))


Output : 

The list of empty dictionaries is : [{}, {}, {}, {}, {}, {}]

Time Complexity: O(n) where n is the number of elements in the list 
Auxiliary Space: O(n), where n is the number of elements in the new res list 

Method #3 : Using itertools

Here is an example of how to use the itertools.repeat() method to initialize a list with empty dictionaries:

Python3




import itertools
 
def init_list_dict(n):
    # using itertools.repeat to repeat the empty dictionary n times
    return list(itertools.repeat({}, n))
 
res = init_list_dict(6)
print("The list of empty dictionaries is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The list of empty dictionaries is : [{}, {}, {}, {}, {}, {}]

This method uses the itertools.repeat() function to repeat the empty dictionary, {} n times and creates a list out of it. This way we can create a list of n empty dictionaries.
This method also creates a new list and assigns it to the variable, so it is not the same as the first two examples.
The time complexity of this method is O(n) and space complexity is O(n)

Method #4: Using a for loop to append empty dictionaries to the list

This program initializes a list containing n empty dictionaries using a for loop and appends an empty dictionary to the list in each iteration. Finally, it prints the resulting list of dictionaries.

Python3




# Initialize list with empty dictionaries using a for loop
n = 6
res = []
for i in range(n):
    res.append({})
print("The list of empty dictionaries is : " + str(res))


Output

The list of empty dictionaries is : [{}, {}, {}, {}, {}, {}]

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

Method #5: Using list comprehension with range function

This will create a list of 6 empty dictionaries using list comprehension with the range function. The _ is used as a placeholder variable as it is not used in the loop.

1-  Initialize an empty list called res
2-   Create a loop to iterate over a range of 6 (0 to 5) using _ as a placeholder variable
For each iteration of the loop, create an empty dictionary using {}
3-  Append the empty dictionary to the list res
4-  Print the string “The list of empty dictionaries is: ” along with the value of res converted to a string         using the str() function
5-  Repeat steps 2-5 for a total of 9 times

Python3




res = [{} for _ in range(6)]
print("The list of empty dictionaries is: " + str(res))


Output

The list of empty dictionaries is: [{}, {}, {}, {}, {}, {}]

Time complexity: O(n), where n is the number of dictionaries in the list, because it is a simple loop through the range function.
Auxiliary space: O(n), as it creates a list of n empty dictionaries in memory.

Method #6: Using the built-in function dict() inside a loop to initialize the list with empty dictionaries.

This method creates a list of 6 empty dictionaries using the dict() function inside a list comprehension. Each iteration of the loop creates a new empty dictionary object, which is then added to the list. This method is more explicit and easier to read than using the {} + ‘*’ operator or list comprehension.

Python3




# Initialize list with empty dictionaries using dict() function
res = [dict() for i in range(6)]
 
print("The list of empty dictionaries is : " + str(res))


Output

The list of empty dictionaries is : [{}, {}, {}, {}, {}, {}]

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

The space complexity of this code is also O(n), where n is the number of elements in the list.



Last Updated : 24 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads