1

I have a list in Python. The list has 5 elements, which can either be "None" or any other string. I want to test if each element is not "None" (that is if all the elements are set).

I've written the following code (where userInventory is the 5-element list) :

 if all(item is not None for item in userInventory):
   print = "Inventory is full, you rat!"
 else:
   ...

For example, ['None', 'Monkey', 'None', 'Jewel', 'None'] should go to the else statement, while ['None', 'None','None','None','None'] should display the message. But right now, the if statement returns true for all the lists...

NOTE The list comes from an HTML form parsed by the cgi library. The data was initially placed by Python and appear like this in HTML : <input type="hidden" name="Inventory1" value="None">

userInventory=[form.getvalue("Inventory1"),form.getvalue("Inventory2"),form.getvalu    e("Inventory3"),form.getvalue("Inventory4"),form.getvalue("Inventory5")]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Justin D.
  • 4,946
  • 5
  • 36
  • 69

1 Answers1

5

is is used for identity check. Since your list contains string 'None' instead of None, you need to use !=:

item != 'None'

Related: Python is operator?

Community
  • 1
  • 1
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • This worked, thanks. I am new to Python. I though `is not` was syntactic sugar for `!=`. I'll read about 'identity check'! – Justin D. Dec 03 '13 at 23:17