Python - how to check if a given number exists in a LIST.

I have a function to test if a given number exists in a list
def inseT1(setT1,actnum):
    if actnum in setT1:
        result1 = 1
    else:
        result1 = 0
    return result1

Open in new window


This function works if I feed it with a list directly- like T1 = [1, 2, 3, 4, 5, 6, 8].

But it does not work if I feed it with a list created indirectly from another list - like
order= [('[8, 9]', '[8, 9, 10, 11]', '[8, 9]')]
T1 = order[0][0]

I attach a cgi page below which illustrates this.  Lines 36 to 42 illustrate the use of this function when it is fed  indirectly.
Lines 44 to 49 illustrate the use of the function when it is fed directly.
Direct feed works, but indirect feed does not. I cannot understand this !!

The indirect feed scenario is what I need for my application. Hopefully the stupid mistake is obvious to you !!

This is the output I get when I run the page below :

0 this is result from using function {'8', ',', '9', ']', ' ', '['} this is setT1 [8, 9] this is T1
1 this is result from using function {1, 2, 3, 4, 5, 6, 8} this is setT1 [1, 2, 3, 4, 5, 6, 8] this is T1
------ CORRECT


Thanks for help
james


#!C:\Python34\python.exe
import cgi,cgitb
import mysql.connector as conn
def htmlTop():
    print("""Content-type:text/html\n\n
        <!DOCTYPE html>
        <html lang="en">
            <head>
                <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
                <title> My Server-side template</title>
        </head>
        <body>""")

def htmlTail():
    print(""" its done</body>
        </html>""")

def connectDB():
    db=conn.connect(host='localhost' ,user='root' ,passwd='844cheminduplan' ,db='office')
    cursor = db.cursor()
    return db, cursor 

def inseT1(setT1,actnum):
    if actnum in setT1:
        result1 = 1
    else:
        result1 = 0
    return result1

#main program
if __name__== "__main__":
    try:
        htmlTop()
        
        actnum=8
        order= [('[8, 9]', '[8, 9, 10, 11]', '[8, 9]')]
        T1 = order[0][0]
        setT1 = set(T1)
        result1=inseT1(setT1,actnum)
        print(result1, "this is result from using function")
        print(setT1, "this is setT1")
        print(T1,"this is T1")
        
        #T1 = [1, 2, 3, 4, 5, 6, 8]
        #setT1 = set(T1)
        #result1=inseT1(setT1,actnum)
        #print(result1, "this is result from using function")
        #print(setT1, "this is setT1")
        #print(T1,"this is T1")
        
        htmlTail()
    except:
        cgi.print_exception()

Open in new window

jameskaneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Problem solved. Since what is inside order[0][0] is a string, you need to eval it to a list before you try to use your function.
import sys

def inseT1(setT1,actnum):
	if actnum in setT1:
	    result1 = 1
	else:
	    result1 = 0
	return result1
                                

order= [('[8, 9]', '[8, 9, 10, 11]', '[8, 9]')]
T1 = eval(order[0][0])  # <-- just apply eval before you send the content to the function

print inseT1(T1,8)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jameskaneAuthor Commented:
solved !

thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.