Dilli Dilli - 8 months ago 55
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?


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

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

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