If Condition with Strings

Hi,

Very short question.

I want to check this condition.

list = ['3', '5', '8']

if list[0] != NA & list[0] != NA & list[0] != NA:
       print 'No NAs'
else:
       print 'there is at least one NA'

But i get the following error:
TypeError: unsupported operand type(s) for &: 'str' and 'str'

What's the shortest way to do what i want to do without nested if else???! don't wanna use nested if else.
dfernanAsked:
Who is Participating?
 
Infinity08Commented:
& is the bitwise AND operator. I guess you intended to use the logical AND operator :

if list[0] != NA and list[1] != NA and list[2] != NA:
       print 'No NAs'
else:
       print 'there is at least one NA'

Open in new window

0
 
LunarNRGCommented:
You should avoid rebinding built-in names. In other words, don't use 'list' as a variable name.

Here's another option ...


l = ['3', '5', '8']
NA = 'NA'
 
if NA not in l:
   print 'No NAs'
else:
   print 'there is at least one NA'
 
# ... or ...
 
if NA in l:
   print 'there is at least one NA'   
else:
    print 'No NAs'
    

Open in new window

0
 
peprCommented:
l (small L) is also not very practical identifier. It can be considered I or 1 depending on the used font. The lst identifier is quite usual in the cases. Otherwise, LunarNRG's solution is fine. Notice also that there is less need to assign

NA = 'NA'

as the 'NA' is used on a single place.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.