+2 votes
in Programming Languages by (71.8k points)
How can I check if a dictionary is empty in pythonic way?

1 Answer

+2 votes
by (71.8k points)
selected by
 
Best answer

There are multiple ways to check if a dictionary is empty in python. Some of them are as follows...

let  y = {}, 

- use not y

- use len(y) == 0

- use bool(y) , it will return false if the dictionary y is empty

>>> y = {}
>>> if not y: print 'Empty'
Empty
>>> if len(y) == 0: print 'empty'
empty
>>> if not bool(y): print 'empty'
empty
>>>


...