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

1 Answer

+1 vote
by (11.2k points)

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
>>>

...