Dilli Dilli - 1 month ago 10
Python Question

Detect Unicode empty value in a dictionary

I have something like this in a list.

my_arr = [{'Brand_Name': u''}, {'Brand_Name':u''}, {'Brand_Name':u'randomstr1'}]


I want to be able to get all the values that are empty for brand name. In thise case I'd get two empty values. I tried this

for dictionary in my_arr:
if None in dictionary:
print dictionary


This doesn't work. How would I get the unicode empty value?

Answer

You can just check for falsey values with if not ... (this will also include None, [], (), etc)

>>> brands = [
        {'Brand_Name': u''},
        {'Brand_Name':u''},
        {'Brand_Name':u'randomstr1'},
    ]
>>> for brand in brands:
        if not brand['Brand_Name']:
            print brand


{'Brand_Name': u''}
{'Brand_Name': u''}
Comments