What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Create Empty Dictionary in Python (5 Easy Ways)

  • Apr 13, 2022
  • 5 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
Create Empty Dictionary in Python (5 Easy Ways)

Dealing with data structure is the most fundamental thing while working with any programming language. Dictionary is one such primary data structure frequently used by every programmer while coding in python. However, there are times when you need to check whether the dictionary is empty or not. To help you with the same, we will discuss some of the common methods to check python empty dictionary along with examples and output. But before that, let's have flashback to python dictionary below. So, let’s get started!

What is Python Dictionary?

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element. Dictionary holds the key: value pair to make it more optimized. Each key-value pair in a Dictionary is separated by a colon(:) whereas each key is separated by a 'comma'. A Dictionary in python is declared by enclosing a comma-separated list of key-value pairs using curly braces {}.

For example:

sample_dict = {
  "fruit": "mango",
  "vegetable": "potato",
  "flower": "rose"
}
print(sample_dict)

 

Output:

{'fruit': 'mango', 'vegetable': 'potato', 'flower': 'rose'}

 

How to Create an Empty Dictionary in Python?

Below are three common methods by which you can create an empty dictionary in python:

1) By declaring empty dictionary

In Python, we can create an empty dictionary by putting no elements inside curly brackets. Check out the below example to better understand this method.

For example:

myDictioanry = {}

print("Created empty dictionary:",myDictioanry)

print(type(myDictioanry))

 

Output:

Created empty dictionary: {}
<class 'dict'>

 

2) Using dict() method

Python empty dictionary means that it does not contain any key-value pair elements. To create an empty dictionary in this example, we can use a dict(), which takes no arguments. If no arguments are provided, an empty dictionary is created.

For example:

myDictionary = dict()

print("Empty dictionary:",myDictionary)
print(type(myDictionary))

 

Output:

Empty dictionary: {}
<class 'dict'>

 

3) Using keys

In Python, we can use the zip() and len() methods to create an empty dictionary with keys. This method creates a dictionary of keys but returns no values from the dictionary.

For examples:

keyList = [8,2,5]
mailcap= dict([(8,5)])

n = dict(zip(keyList, [None]*len(keyList)))
print(n)

 

Output:

{8: None, 2: None, 5: None}

 

4) Create an empty nested dictionary

In this method, we use the dict() method concept to create an empty nested dictionary. The dict() method is a Python built-in function that accepts no arguments.

For example:

myDictionary = { '': { }, 
         '': { }}
print("Empty Nested dictionary:",myDictionary)

 

Output:

Empty Nested dictionary: {'': {}}

 

5) Check if the python dictionary is empty

The “if condition” can be used to determine whether or not the Python dictionary is empty. It will determine whether or not the dictionary contains elements. If the dictionary is empty, the “if condition” returns true; otherwise, it returns false.

For example:

myDictionary = {1:"b",9:"c",2:"p"}
newDictionary = {}

if myDictionary:
   print("myDictionary is not empty")
else:
   print("myDictionary is empty")
if newDictionary:
   print("newDictionary is not empty")
else:
   print("newDictionary is empty")

 

Output:

myDictionary is not empty
newDictionary is empty

 

Also, you can use the “not operator”, “bool(dict)” or “len()” method instead of “if condition” to check the python empty dictionary.

Conclusion

Dictionary plays an important role while coding in python. In this article, we understood how you can check whether the dictionary you are working with is empty or not. We learned five explicit methods to check the python empty dictionary with various examples and output. We recommend you to use all these methods efficiently to make your coding easy and efficient.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.