This article will discuss four methods for checking if a list within a dictionary is empty. Before that, let’s briefly review how to access items in the Python dictionary as a prerequisite.
Accessing Elements of a Python Dictionary
Items of a Python dictionary can be accessed using the dictionary keys. This can be done using the square brackets or <dict>.get(<key>) function as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Define a dictionary with three keys. data1 = { "schools": ["Braeburn", "Zetech", "TUB", "Yale", "Warsaw"], "teachers": [], "other_info": {"fees": [3450, 2800, 4680, 6200, 5100], "cities": []}, } # Example 1: Access the values of the "schools" key schools = data1["schools"] print(schools) schools = data1.get("schools") print(schools) # Example 2: Accessing values for nested dictionary. fees = data1["other_info"]["fees"] print(fees) fees = data1.get("other_info").get("fees") print(fees) |
Output:
['Braeburn', 'Zetech', 'TUB', 'Yale', 'Warsaw'] ['Braeburn', 'Zetech', 'TUB', 'Yale', 'Warsaw'] [3450, 2800, 4680, 6200, 5100] [3450, 2800, 4680, 6200, 5100]
The main difference between the two methods: using <dict>[<key>] raises an error if the key being accessed does not exist, but <dict>.get(<key>) will return None.
Checking If the List within a Dictionary is Empty
This Section covers four methods for this purpose.
Method 1: Using if-statement
An empty list in Python is considered falsy. That means “if <list>” returns False if <list> is empty; otherwise, it returns True. Let’s see an example.
1 2 3 4 5 6 7 8 9 10 |
data1 = { "schools": ["Braeburn", "Zetech", "TUB", "Yale", "Warsaw"], "teachers": [], "other_info": {"fees": [3450, 2800, 4680, 6200, 5100], "cities": []}, } # not <list> returns True if <list> is empty. if not data1["teachers"]: print("The list is empty.") else: print("The list is not empty.") |
Output:
The list is empty.
Method 2: Using the bool() function
The bool(<obj>) evaluates the object passed to it as either True or False. If an empty list is passed, bool(<obj>) evaluates to False; otherwise, it evaluates to True. For example,
1 2 3 4 5 6 7 |
data1 = { "schools": ["Braeburn", "Zetech", "TUB", "Yale", "Warsaw"], "teachers": [], "other_info": {"fees": [3450, 2800, 4680, 6200, 5100], "cities": []}, } print("Check if empty (False if empty): ", bool(data1["other_info"]["fees"])) print("Check if empty (true if not empty): ", bool(data1["teachers"])) |
Output:
Check if empty (False if empty): True Check if empty (true if not empty): False
Method 3: By checking its length
The length of a list can be determined using the inbuilt len() function. If the size of a list is zero, then it is effectively an empty list; otherwise, the length of the list is equal to the number of elements in it. For example,
1 2 3 4 |
lst1 = ["Ammon", 45, "Still"] print(len(lst1)) # 3 lst2 = [] print(len(lst2)) # 0 |
With that in mind, we can now check if a list within a dictionary is empty using len() and if-statement, as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
data1 = { "schools": ["Braeburn", "Zetech", "TUB", "Yale", "Warsaw"], "teachers": [], "other_info": {"fees": [3450, 2800, 4680, 6200, 5100], "cities": []}, } if len(data1["schools"]) > 0: print("List is not empty.") else: print("List is empty.") if len(data1["other_info"]["cities"]) > 0: print("The list is not empty.") else: print("The list is empty.") |
Output:
The list is not empty. The list is empty.
Method 4: By comparing it to an empty list
We can also check if a list is empty by comparing it with a truly empty list using the comparison operator (==). If the list (say A) being checked is empty, then A==[ ] will return True; otherwise, it will return False.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
data1 = { "schools": ["Braeburn", "Zetech", "TUB", "Yale", "Warsaw"], "teachers": [], "other_info": {"fees": [3450, 2800, 4680, 6200, 5100], "cities": []}, } if data1["schools"] == []: print("List is not empty.") else: print("List is empty.") if data1["other_info"]["cities"] == []: print("List is not empty.") else: print("List is empty.") |
Output:
The list is empty. The list is not empty.
Conclusion
In this post, we learned four approaches to checking if a list within a dictionary is empty. The methods discussed include: using if-statement, checking the length of the list, using the bool() method, and comparing the list in question with an empty list.