In this Python tutorial, we will discuss How to create an empty Python dictionary Here we will also cover the below examples:
- How to create an empty dictionary in Python with keys
- How to create empty nested dictionary Python
- How to check if Python dictionary is empty
How to create an empty Python dictionary
- In Python to create an empty dictionary, we can assign no elements in curly brackets {}.
- We can also create an empty dictionary by using the dict() method it is a built-in function in Python and takes no arguments.
Example:
Let’s take an example and check how to create an empty dictionary in Python
my_new_dict = {}
print("Created empty dictionary:",my_new_dict)
print(type(my_new_dict))
So, with this above code, we create an empty dictionary that is ‘my_new_dict’ and it is equal to curly brackets{}. Now if you want to see an empty dictionary in output then you have to use a print statement. It will display an empty dictionary and type of dictionary.
Here is the Output of the following given code.
Another example to create an empty dictionary by using dict() built-in function
In Python, an empty dictionary means that it does not contain key-value pair elements. In this example to create an empty dictionary, we can use a dict constructor and this method takes no arguments. If no argument is passed then it creates an empty dictionary.
Source Code:
emp_num = dict()
print("Empty dictionary:",emp_num)
print(type(emp_num))
This is how to create an empty dictionary in Python.
Read: Python Dictionary to CSV
How to create an empty dictionary in Python with keys
In Python to create an empty dictionary with keys, we can use the combination of zip() and len() method. This method will initialize a dictionary of keys and returns no values in the dictionary.
Source Code:
new_key_lis = [18,23,45]
b= dict([(23,18)])
c = dict(zip(new_key_lis, [None]*len(new_key_lis)))
print(c)
Here is the Implementation of the following given Code
Read: Python convert dictionary to an array
How to create empty nested dictionary
- Here we can see how to create an empty nested dictionary in Python.
- In this example, we use the concept of the dict() method to create an empty nested dictionary. The dict() method is a built-in function in Python and it takes no arguments.
Example:
new_dict = { '': { },
'': { }}
print("Empty Nested dictionary:",new_dict)
Here is the execution of the following given code
Read: Get all values from a dictionary Python
How to check if Python dictionary is empty
- To check if the Python dictionary is empty or not we can use the if condition. It will check the condition if the dictionary contains elements or not. The if condition will return true if the dictionary is empty otherwise it will return false.
- There are various method to solve this task
- By using if condition
- By using not operator
- By using bool(dict) method
- By using len() method
By using if condition
In Python the, ‘if’ condition verifies if the dictionary contains elements or not. So in the below code, we will check the condition.
Source Code:
my_dictionary = {18:"h",19:"a",25:"o"}
new_dictionary = {}
if my_dictionary:
print("my_dictionary is not empty")
else:
print("my_dictionary is empty")
if new_dictionary:
print("new_dictionary is not empty")
else:
print("new_dictionary is empty")
In the above code first, we will initialize two dictionaries. The first dictionary contains key-value pair and the second dictionary creates an empty dict. Now use the if condition and check which dictionary is empty. To do this we use the if-else condition and if the dictionary contains an element then it will display the dictionary is not empty.
Here is the Screenshot of the following given code
Read: Python dictionary of tuples
By using not operator
In Python to applying the not operator in a dictionary, we will first initialize an empty dictionary and use the ‘not’ operator to check if the dictionary is empty.
Example:
Let’s take an example and check if the Python dictionary is empty or not
my_emp_dict = {}
new_out = not my_emp_dict
print("Dictionary is epty or not?:",new_out)
Output:
Read: Python creates a dictionary from two lists
By using bool() dict method
To check if the dictionary is not empty then it will return true by using the bool() method. This method is used to return a boolean value that means it will display in the output True or False and it takes only one argument.
Source Code:
my_new_dict = {}
emp_val = bool(my_new_dict)
print(emp_val)
Here is the execution of the following given code
Read: How to Create a Dictionary from one list in Python
By using len() method
An alternative way to check if the dictionary is empty or not in Python is by using the len() method.
Source Code:
you_dictionary = {}
new_emp_dict = len(you_dictionary)
if new_emp_dict == 0:
print ("Empty dictionary")
else:
print ("Dictionary is not empty")
In the above code first, we will initialize an empty dictionary ‘you_dictionary’. After that, we use the len() function to calculate the length of the dictionary and contain it in the variable ‘new_emp_dict’ and use the if statement condition to check if the dictionary is empty or not.
Here is the implementation of the following given code
You may also like reading the following tutorials.
- Python dictionary pop
- Python loop through a list
- Python for loop index
- Python find max value in a dictionary
- Python dictionary multiple keys
In this Python tutorial, we have discussed How to create an empty Python dictionary Here we have also covered the following topics:
- How to create an empty dictionary in Python with keys
- How to create empty nested dictionary Python
- How to check if Python dictionary is empty
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.