In this Python list program, we will take a user input as a list and count empty dictionaries from the given list with the help of the below-given steps.
Count empty dictionaries in a list:
Steps to solve the program
- Take user input as list containing dictionaries, lists, and tuples.
- Create an empty list.
- Use for loop to iterate over the given list.
- Use isinstance function to check whether the element in the list is a dictionary or not if yes and that dictionary is empty add that dictionary to the empty list.
- Count the number of empty dictionaries.
- Print the result to see the output.
#Input list
list1 = [{}, {"a": "sqatools"}, [], {"a": 123}, {}, {}, ()]
#Empty list
list2 = []
for element in list1:
#Checking for dictionary
if isinstance(element, dict):
if len(element) == 0:
list2.append(element)
#Printing output
print(len(list2))
Output :
3
Related Articles
Python program to remove consecutive duplicates of given lists.
Python program to pack consecutive duplicates of given list elements into sublists.
Python program to insert items at a specific position in the list.
Python program to select random numbers from the list.
Python program to create a 3*3 grid with numbers.