Sometimes, we want to initialize a dictionary of empty lists in Python.
In this article, we’ll look at how to initialize a dictionary of empty lists in Python.
How to initialize a dictionary of empty lists in Python?
To initialize a dictionary of empty lists in Python, we can use dictionary comprehension.
For instance, we write:
data = {k: [] for k in range(2)}
print(data)
to create a dictionary with 2 entries that are both set to empty lists as values.
Therefore, data
is {0: [], 1: []}
.
Conclusion
To initialize a dictionary of empty lists in Python, we can use dictionary comprehension.