-6

I have list that contains some value now i want to check it for empty string and Null values. So i am using below code for same but output is not coming correct. On checking for '' empty string it is giving output 'not nice' Its working fine for 'Null' value but not considering it for '' empty values. Any help would be appreciated.

y=['','']
if ('Null' or '') in y:
    print ('nice')
else:
    print('not nice')
DYZ
  • 55,249
  • 10
  • 64
  • 93
damini chopra
  • 133
  • 2
  • 12
  • Does this answer your question? [How to check if one of the following items is in a list?](https://stackoverflow.com/questions/740287/how-to-check-if-one-of-the-following-items-is-in-a-list) – DYZ Nov 11 '19 at 06:09
  • 2
    `'Null' or ''` is evaluated to `'Null'`. so your code is equivalent to `'Null' in y` – ymonad Nov 11 '19 at 06:10

3 Answers3

-2

try the below code.

y = ['', '']
if 'Null' in y or '' in y:
    print('nice')
else:
    print('not nice')
Karmveer Singh
  • 939
  • 5
  • 16
-2

or evaluates two Boolean expressions. In your case, the statement in the bracket is evaluated first and then the in is evaluated. This leads to the if statement being interpreted as false. You do not need to use the brackets. Just do this:

if 'Null' in y or '' in y:

Now there's a Boolean expression on either side of the or, so it will evaluate properly.

Seananigan
  • 43
  • 4
csoham
  • 140
  • 7
-3

You can try something like below:

a = ["Hello","","World"]
for words in a:
    if (words):
        print ("Nice")
    else:
        print("Not nice")

Nice
Not nice
Nice
Vikash Kumar
  • 177
  • 1
  • 9