If you work with dictionaries, sometimes, you will have to check whether a dictionary is empty.
In programming, usually, there are many ways to get the same result. In this lesson, I’ll show you a few ways, you can achieve this goal.
The first way is the simplest, but the other three are also not very complicated. In our example, we are going to have three dictionaries: with a value, without a value, and with None.
Checking if directory value is empty
First example:
In order to use the first example, use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
my_dict = {'age': 34} empty_dict = {} none_dict = None if not my_dict: print("Dict is empty") else: print("Dict is not empty") if not empty_dict: print("Dict is empty") else: print("Dict is not empty") if not none_dict: print("Dict is empty") else: print("Dict is not empty") |
The my_dict dictionary has a value, and the empty_dict doesn’t have any, therefore adequate messages are returned.
Dict is not empty Dict is empty Dict is empty
Second example:
The second example has a bit longer notation. In this case, we are going to use the bool function. Just add bool after not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
my_dict = {'age': 34} empty_dict = {} none_dict = None if not bool(my_dict): print("Dict is empty") else: print("Dict is not empty") if not bool(empty_dict): print("Dict is empty") else: print("Dict is not empty") if not bool(none_dict): print("Dict is empty") else: print("Dict is not empty" |
The result is the same for all examples. This is a less pythonic way to get the same result.
Third example:
In the third example, we are not going to use bool. Instead, we will try a different function, called len. This function returns the number of elements inside an object. If len(dict) == 0, then the object is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
my_dict = {'age': 34} empty_dict = {} none_dict = None my_dict_len = len(my_dict) empty_dict_len = len(empty_dict) none_dict_len = len(empty_dict) if my_dict_len == 0: print("Dict is empty") else: print("Dict is not empty, it has " + str(my_dict_len) + " element(s)") if empty_dict_len == 0: print("Dict is empty") else: print("Dict is not empty, it has " + str(empty_dict_len) + " element(s)") if none_dict_len == 0: print("Dict is empty") else: print("Dict is not empty, it has " + str(empty_dict_len) + " element(s)") |
If the directory is not empty, then the code will return the number of directory elements. Apart from that, the code returns the same result as the one before.
Fourth way:
There is also a fourth way and this code returns a bit different result than before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
my_dict = {'age': 34} empty_dict = {} none_dict = None if my_dict == {}: print("Dict is empty") else: print("Dict is not empty") if empty_dict == {}: print("Dict is empty") else: print("Dict is not empty") if none_dict == {}: print("Dict is empty") else: print("Dict is not empty") |
This code returns the following result:
Dict is not empty Dict is empty Dict is not empty
Notice, that the last part of the code treats the None value differently than the examples before. If the dictionary is None, instead of empty, it will return that the directory is not empty.