Testing for Null?

Terry Reedy tjreedy at udel.edu
Sun Jun 29 02:06:41 CEST 2008

Alex Bryan wrote:
> I am having a problem with a list value that is empty. I have a list of 
> definitions called mainList. the 5th value in the list doesn't have 
> anything in it. In this case, the values are definitions; also, in this 
> case just the word cheese is defined. Here is my output to the console:
> 5.   a sprawling,weedy plant having small lavender or white flowers and 
> round, flat, segmented fruits thought to resemble little wheels of cheese.
> 6.  
> 7.  an ingot or billet made into a convex, circular form by blows at the 
> ends.
> I've made it so where the numbers, the period, and two spaces follow 
> that, then the definition. However, as you can see in 6, there is 
> nothing. Here is the code to print all this:
> n=0
> for x in mainList:
>     if mainList[n] == "":
>         print "Error on this definition"
>     else:
>         print str(n+1)+".  "+str(mainList[n])
>     n=n+1
> Now the two "" is where I need to figure out if it is empty. What is up 
> right now doesn't work; or at least doesn't give the desired result. So 
> I need to know how to write the if statement to make it work. This 
> should be simple, but I just don't know how to do it, never had this 
> problem before.

My guess is that your 'empty' value is something like ' ', which is not 
equal to ''.  Change str to repr and see better what item 6 is.

More information about the Python-list mailing list