×

How to check if the dictionary is empty in Python?

What is a dictionary in Python?

A directory is a collection of data but data is not ordered. Unlike other data types, it does not hold a single value as its element, it holds elements in the format of key-value pair, where keys and values are separated by ‘:’ and two keys are separated by a comma. A dictionary can have identical values but it cannot store identical keys.

While creating a dictionary we should keep in mind that keys are case sensitive i.e., both ‘day’ and ‘Day’ will be considered as different keys as the first letter of both words is in a different case (small letter and capital letter).

Example 1:

# Creating an empty Dictionary  
dic1={} 
print("Example of an empty dictionary: \n ") 
print(dic1,"\n”) 
    
# Creating a Dictionary with different keys
dic1={'name': 'Amit', 1: [8, 1, 20, 4]} 
print("A normal dictionary using different Keys: \n ") 
print(dic1) 

Output:

Example of an empty dictionary: 
 {} 
A normal dictionary using different Keys: 
{'name': 'Amit', 1: [8, 1, 20, 4]}

Using the bool() function to check if a dictionary is empty

To perform this task, we use the bool function. This function converts the passed object to a Boolean value, but it is only possible if and only if the string is not empty. If an empty string is passed to this function, it returns false, as an empty string cannot be converted.

Example 2:

# Initializing empty dictionary
dict1={}
 
# Printing original dictionary with all its contents
print("The original dictionary : \n "+str(dict))
 
# Using thebool() function
# to checkthe status of the dictionary
rslt=notbool(dict1)
 
# Returning status of the dictionary
print("\nStatus of dictionary : \n "+str(rslt))


nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}


print("\nThe original dictionary : \n "+str(nw_dict))
 
# Using thebool() function
# to check the status of the dictionary
rslt=notbool(nw_dict)
 
# Returning status of the dictionary
print("\nStatus of dictionary : \n "+str(rslt))

Output:

How to check if the dictionary is empty in Python

Using the not operator to check the status of the dictionary

Not operator can also be used to check the status of the dictionary, after using this operator it returns true if the dictionary is found out to be empty.

Example 3:

# Initializing empty dictionary
dict1={}
 
# Printing original dictionary with all its contents
print("The original dictionary : \n"+str(dict1))
 
# Using the not operator
# to check the status of the dictionary
rslt=notdict1
 
# Returning status of the dictionary
print("\nIs dictionary empty ? : \n "+str(rslt))


nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}


# Printing original dictionary with all its contents
print("\nThe original dictionary : \n "+str(nw_dict))
 
# Using the not operator
# to check the status of the dictionary
rslt=notnw_dict
 
# Returning status of the dictionary
print("\nIs dictionary empty ? : "+str(rslt))

Output:

How to check if the dictionary is empty in Python

Using the len() function to check the status of the dictionary:

The len() function is also used to check the status of the dictionary when the dictionary is passed to this function, it returns its length and compares it and hence returns the result as true or false.

Example 4:

# Initializing empty dictionary
dict1={}
 
# Printing original dictionary with all its contents
print("The original dictionary : \n "+str(dict1))
 
# Using the len() function
# to checkthe status of the dictionary
rslt=len(dict1) ==0 


# Returning status of the dictionary
print("\nIs dictionary empty ? : \n "+str(rslt))


nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}


# Printing original dictionary with all its contents
print("\nThe original dictionary : \n "+str(nw_dict))
 
# Using the len() function
# to check the status of the dictionary
rslt=len(nw_dict) ==0 


# Returning status of the dictionary
print("\nIs dictionary empty ? : \n "+str(rslt))

Output:

How to check if the dictionary is empty in Python

Using the Equality operator to check the status of the dictionary

 While using this method, we take an additional empty dictionary to compare it with the original one. And after comparing it, the result is returned as “True” or “False”.

Example 5:

# Initializing a dictionary check it against an empty dictionary
nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}
# Initializing empty dictionary
tst_dict={}
 
# Printing original dictionary with all its contents
print("The original dictionary :\n  "+str(nw_dict))
 
# Using equality operator
# to check the status of the dictionary
rslt=tst_dict==nw_dict
 
# Returning status of the dictionary
print("\nIs dictionary empty ? : "+str(rslt))

Output:

The original dictionary :
‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'


Is dictionary empty ? : False

Related Topics

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Fsolve in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Add Dictionary to Dictionary in Python

Dictionary in python: Python's execution of a data model, known more commonly as an implicit array, is a dictionary. A dictionary is made up of a group of key-value pairs. Each...

4 minutes read.

How to Substring a String in Python

String Unicode characters collection is referred to as a String. It consists of a succession of characters, including alphanumeric and special characters. Substring: Core Concept A substring is a piece of a string....

5 minutes read.

Convert string into int in Python

String A string is defined as a series of characters, special characters, and numbers. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements...

2 minutes read.

Python Bubble Sort

Bubble sort is one of the techniques used to sort the elements in lists in a certain order, either in ascending or descending order. It is called Bubble sort because...

4 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

Cursor in Python

The cursor is an item that aids in query execution and records retrieval from databases. The cursor is crucial to the execution of the query. In-depth information on the execution...

7 minutes read.

Read JSON File in Python

JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the...

5 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Python Keywords

If you are trying to learn a programming language, you need to have a basic idea of "What are keywords" and "How they are used". You can learn about keywords in...

7 minutes read.

Python Multiprocessing Processor

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 minutes read.