Python Practice › List Programs › Program 53
Count empty dictionaries
Write a Python program to count the empty dictionaries in a list (ignoring other empty containers).
Input
[{}, {"a": "sqatools"}, [], {}, {"a": 123}, {}, {}, ()]Output
4
Approach
- Count items that are dicts and empty.
Solution
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.