love2code love2code - 2 years ago 68
Python Question

Why comparing empty values is not working?

I have a list of dictionaries.

students = [{"id":''},{"id":1},{"id":3}]

I am iterating through this and looking for dictionaries where id is not

Here is what I have tried:

for student in students:
if(student['id'] is not None or student['id'] != ''):
print("found student" + str(student['id']))

But no matter what, it always goes inside the
block. Any pointers what is wrong in comparing blank values?

Answer Source

What about:

if student['id'] not in (None, ''):
    # do someting
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download