Python: Create a list of empty dictionaries
Create List of Empty Dictionaries
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:
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics