How to Create an Empty Dictionary in Python



Python


In this article, we show how to create an empty dictionary in Python.

A dictionary in Python is really an associative array or hash table that is composed of key-value pairs. So if you have a dictionary called itemprices, one key may be "T-shirt" with a value of 24.95, another key may be "Brief" with a value of 14.95, and another key may be "Pants" with a value of 34.95.

Though most times, you would probably create a dictionary with elements in them, other times you may want to create an empty dictionary.

An empty dictionary is a dictionary that contains no items (no key:value pairs).

You may create an empty dictionary if you want a user to populate the dictionary.

For example, you create an empty dictionary and then receive input from a user and then populate the dictionary based on the user input.

So now we'll go over how to create an empty dictionary in Python.


How to Create an Empty Dictionary in Python

To create an empty dictionary, the dictionary should be set to {}.

This is shown in the code below.



So, with this code above, we create a dictionary called items which is set equal to {}. So this dictionary named items is empty.

Another way of creating an empty dictionary is by setting the dictionary equal to dict().

This is shown in the code below.



So, this dictionary named items is an empty dictionary.

And this is all that is required to create an empty dictionary in Python.


Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python



HTML Comment Box is loading comments...