Python Create Empty Dictionary

In this Python tutorial, You will learn about Python to create an empty dictionary.

In my data science project, I had to write a program to count the number of words in the text and store them with their frequencies. To accomplish that, I created an empty dictionary that stores each word as a key and its frequency as a value.

So, in this tutorial, I have explained several methods, such as using curly braces {}, dict(), etc, including one I used in the project to create an empty dictionary.

Python Create Empty Dictionary

An empty dictionary is a dictionary without any values. Generally, a dictionary stores the key-value pairs, where the keys are unique and associated with a specific value.

For example, you have a profile dictionary such as profile = {“name”:”Dalton”, “email”: “Dalton@gmail.com”}, But this dictionary is not empty. To make it empty, initialize it but don’t include keys and values like this profile ={}.

Now that you know what an empty Python dictionary means, let’s examine several methods for creating one.

How to Create an Empty Dictionary in Python Using {}

To create an empty dictionary, use the curly braces {} containing the key-value pairs.

For example, use the code below to create an empty dictionary named ‘user’, as shown below.

user = {}
Python Create Empty Dictionary

Look at the output; when you print the dictionary ‘user’, it shows like {}, now, which means the user is an empty dictionary.

READ:  Constructor in Python [Complete Guide]

This is how to create an empty dictionary in Python using the curly braces {}.

Python Empty Dictionary Using Dict() Function

Python provides a method called Dict(), a constructor that takes no arguments and creates an empty dictionary.

For example, let’s create an empty dictionary named ‘profile’ as shown below.

profile = dict()

print(profile)
Python Empty Dictionary Using Dict() Function

Again, you have created the empty dictionary named ‘profile’ but using Python’s dict() method, as seen in the above output.

This is how to create an empty dictionary in Python using Python’s dict() method.

How to Create an Empty Dictionary in Python Using Dictionary Comprehension

In Python, dictionary comprehension provides a straightforward way to create dictionaries. Although it’s more commonly used to create populated dictionaries, it can also create an empty dictionary in Python.

For example, you want to create an empty dictionary named states_and_capitals, which stores states’ names and capitals. Later, you will add the key-value pairs dynamically.

states_and_capitals = {key: value for key, value in []}

print(states_and_capitals)
How to Create an Empty Dictionary in Python Using Dictionary Comprehension

From the output, the empty dictionary ‘states_and_capitals’ is created using dictionary comprehension.

Let’s understand the dictionary comprehension part ‘{key: value for the key, value in []}’, here within curly braces {}, for loop iterates over empty list [] and access each key, value but as the list is empty so it is iterating over nothing and creating an empty dictionary.

Here, key:value represents the key-value pairs of the dictionary that are generated if the list [] contains the items. In this case, list [] is empty, creating an empty dictionary.

This is how to create an empty dictionary in Python using the concepts of dictionary comprehension.

READ:  How to convert Python tuple to dictionary

Conclusion

In this Python tutorial, you learned about Python to create an empty dictionary using the curly braces, dict() method and dictionary comprehension.

You may like to read: