Categories
Python Answers

How to initialize a dictionary of empty lists in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *