w3resource

Python: Create a list of empty dictionaries

Python List: Exercise - 61 with Solution

Write a Python program to create a list of empty dictionaries.

Sample Solution:

Python Code:

# Define a variable 'n' and set it to the value 5
n = 5

# Create a list 'l' using a list comprehension, where each element is an empty dictionary
# The list comprehension creates 'n' empty dictionaries, and the list 'l' contains these dictionaries
l = [{} for _ in range(n)]

# Print the list 'l' containing 'n' empty dictionaries
print(l)

Sample Output:

[{}, {}, {}, {}, {}]

Flowchart:

Flowchart: Create a list of empty dictionaries

Python Code Editor:

Previous: Write a Python program to find a tuple, the smallest second index value from a list of tuples.
Next: Write a Python program to print a list of space-separated elements.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.