Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick and easy way to check if all items in a dictionary are empty strings?

Tags:

python

I have a dictionary of N items. Their values are strings, but I'm looking for an easy way to detect if they are all empty strings.

{'a': u'', 'b': u'', 'c': u''}
like image 978
speg Avatar asked Dec 13 '22 08:12

speg


2 Answers

not any(dict.itervalues())

Or:

all(not X for X in dict.itervalues())

Whichever you find clearer.

like image 54
Cat Plus Plus Avatar answered Apr 06 '23 01:04

Cat Plus Plus


Try this:

>>> d={'a':'', 'b':'', 'c':''}
>>> any(map(bool, d.values()))
False
>>> d={'a':'', 'b':'', 'c':'oaeu'}
>>> any(map(bool, d.values()))
True
like image 21
Ishpeck Avatar answered Apr 06 '23 00:04

Ishpeck