Python Practice › List Programs › Program 53

Count empty dictionaries

EasyList Programs · 53 of 100

Write a Python program to count the empty dictionaries in a list (ignoring other empty containers).

Advertisement
Input
[{}, {"a": "sqatools"}, [], {}, {"a": 123}, {}, {}, ()]
Output
4

Approach

  1. Count items that are dicts and empty.

Solution

Python
data = [{}, {"a": "sqatools"}, [], {}, {"a": 123}, {}, {}, ()]print(sum(1 for x in data if isinstance(x, dict) and not x))
Output
4
Note

There are 4 empty dicts in this list; the original sample answer said 3.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Leave a Comment