checking for an item in a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    checking for an item in a list

    I usually use
    [code=python]
    check=False
    for i in the listA:
    if i==value
    check= True
    [/code]
    for checking whether a given values in contained in a list. How can I implement if I define within the context of the original list class with a presumed body of the list method __contains__(se lf,value).
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by python101
    I usually use
    [code=python]
    check=False
    for i in the listA:
    if i==value
    check= True
    [/code]
    for checking whether a given values in contained in a list. How can I implement if I define within the context of the original list class with a presumed body of the list method __contains__(se lf,value).
    [CODE=python]
    aList = [1, 2, 3]
    if 1 in aList:
    print "it's in there"[/CODE]

    Comment

    Working...