53. Problem to count empty dictionaries from the given list.

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
  1. Take user input as list containing dictionaries, lists, and tuples.
  2. Create an empty list.
  3. Use for loop to iterate over the given list.
  4. 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.
  5. Count the number of empty dictionaries.
  6. 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

Items that start with a specific character

Remove consecutive duplicates of given lists

Leave a Comment