love2code love2code - 7 months ago 9
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']))
break


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

Answer

What about:

if student['id'] not in (None, ''):
    # do someting
Comments