Downside Formulation and Resolution Overview
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.
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!
At college, I discovered my love of writing and coding. Each of which I used to be ready to make use of in my profession.
Through the previous 15 years, I’ve held quite a few positions equivalent to:
In-house Company Technical Author for varied software program packages equivalent to Navision and Microsoft CRM
Company Coach (employees of 30+)
Programming Teacher
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder