Wednesday, May 1, 2024
HomePython5 Methods to Verify If a Dictionary is Empty in Python –...

5 Methods to Verify If a Dictionary is Empty in Python – Finxter


Downside Formulation and Resolution Overview

This text will present you the right way to examine if a Dictionary is empty in Python.

Earlier than shifting ahead, let’s overview what a Python Dictionary is.

💡Dictionary Overview: The Python Dictionary is an information construction that homes key:worth pairs of information. This knowledge construction does not enable duplicate keys, thus guaranteeing distinctive key knowledge. An instance of a Dictionary containing knowledge is proven under.

students_grade5 = {'Amy': 11, 'Micah': 10, 'Chloe': 11}

As seen above, the worth portion of the key:worth pair permits duplicates.


💬 Query: How would we write code to examine if a Dictionary is empty?

We are able to accomplish this process by one of many following choices:


Technique 1: Use if/else assertion

This instance makes use of an if/else assertion to examine if a Python Dictionary is empty.

students_grade6 = {}

if (students_grade6):
    print('Dictionary Accommodates Information!')
else:
    print('Dictionary Empty!')

The above code declares an empty Dictionary and saves this to students_grade6. If output to the terminal, the next shows.

Subsequent, an if/else assertion is created. The if assertion checks the Dictionary. Does it comprise any knowledge, or is it empty? As you possibly can see from the above output, the Dictionary is empty.

Subsequently, the print assertion contained in the else assertion executes.


Technique 2: Use a One-Liner

This instance makes use of a one-liner. Often known as the ternary operator to examine if a Python Dictionary is empty.

students_grade6 = {'Mary': 13}
print('Dictionary Accommodates Information!') if students_grade6 else print('Dictionary Empty!')

The above code declares a Dictionary containing one (1) key:worth pair and saves the contents to students_grade6. If output to the terminal, the next shows.

Subsequent, a ternary operator is used to find out if students_grade6 comprises knowledge. The output is predicated on the result and despatched to the terminal.

Dictionary Accommodates Information!

Technique 3: Use len()

This instance makes use of Python’s built-in len() operate to examine if a Python Dictionary is empty.

students_grade6 = {}

if len(students_grade6) == 0:
    print('Dictionary Empty!')

The above code declares an empty Dictionary and saves the contents to students_grade6. If output to the terminal, the next shows.

Subsequent, an if assertion is asserted, calls len(), and passes one (1) argument, students_grade6. This determines the size of the Dictionary.

For the reason that Dictionary is empty, the next is output to the terminal.


Technique 4: Use == Operator

This instance makes use of Python’s == (equal to operator) to examine if a Python Dictionary is empty.

students_grade6 = {}

if (students_grade6 == 0):
    print('Dictionary Empty!')

The above code declares an empty Dictionary and saves the contents to students_grade6. If output to the terminal, the next shows.

Subsequent, an if assertion is asserted and makes use of the == (equal to) operator to find out the size of the Dictionary students_grade6.

For the reason that Dictionary is empty, the next is output to the terminal.


Technique 5: Use Boolean operator

This instance is similar to Technique 2. Nevertheless, this instance wraps a variable contained in the bool() operate to examine if a Python Dictionary is empty.

students_grade6 = {'Mary': 13}
print('Dictionary Accommodates Information!') if bool(students_grade6) else print('Dictionary Empty!')

The above code declares a Dictionary containing one (1) key:worth pair and saves the contents to students_grade6. If output to the terminal, the next shows.

Subsequent, a ternary operator is used to find out if students_grade6 comprises knowledge. The output is predicated on the result of bool(students_grade6) (True or False) and sends the consequence to the terminal.

Dictionary Accommodates Information!

Abstract

This text has offered 5 (5) methods to examine if a Python Dictionary is empty to pick out the perfect match on your coding necessities.

Good Luck & Blissful Coding!


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments