Dilli Dilli - 1 year ago 86
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 Source

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''}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download