Re: Testing for Null?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c0mrade

    Re: Testing for Null?


    Try something like this...

    list = ['lkdfjsldk', None, '', '0', 'slfkjsdlfj', 'lsdgjdlfg', False, True]
    for n, it in enumerate(list) :
    if not it: print 'Error on this definition'
    else: print '%d. %s' % (n+1, it)

    Results:
    1. lkdfjsldk
    Error on this definition
    Error on this definition
    4. 0
    5. slfkjsdlfj
    6. lsdgjdlfg
    Error on this definition
    8. True


    Alexnb 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(mainL ist[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.
    >
    --

    >
    --
    View this message in context: http://www.nabble.com/Testing-for-Nu...p18176481.html
    Sent from the Python - python-list mailing list archive at Nabble.com.

  • Chris

    #2
    Re: Testing for Null?

    On Jun 29, 3:12 am, c0mrade <jacksinglet... @gmail.comwrote :
    Try something like this...
    >
    list = ['lkdfjsldk', None, '', '0', 'slfkjsdlfj', 'lsdgjdlfg', False, True]
    for n, it in enumerate(list) :
        if not it: print 'Error on this definition'
        else: print '%d. %s' % (n+1, it)
    >
    Results:
    1. lkdfjsldk
    Error on this definition
    Error on this definition
    4. 0
    5. slfkjsdlfj
    6. lsdgjdlfg
    Error on this definition
    8. True
    >
    >
    >
    Alexnb 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(mainL ist[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.
    >>
    --
    View this message in context:http://www.nabble.com/Testing-for-Nu...p18176481.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    myList = ['lkdfjsldk', None, '', '0', 'slfkjsdlfj', 'lsdgjdlfg',
    False, True]
    ['%s\t%s'%((i+1) ,element) if element else '%s\tError on this
    definition'%(i+ 1) for i,element in enumerate(myLis t)]

    Comment

    Working...