comparing results of fetchall() with a known value

michal èernoeviè michal.cernoevic at
Tue Aug 28 16:08:20 CEST 2001

how about this:

simple_list = map((lambda x: x[0]), list_of_tuples)


Jeff <jhardcastle at> píse v diskusním
pøíspìvku:e33814e8.0108201528.57883ea1 at
> Hi - I'm new to Python, and am having trouble reading the results of a
> database call using fetchall().  Here's my situation:
> I have a database table containing a list of names, and I have a
> string variable containing a name.  I need logic that will determine
> if my string exists in the database table.  So I have a simple sql
> like "select name from employees", and I have a string empname =
> 'Jones', and I need to verify that 'Jones' is in the employees list
> (assume duplicate names are not an issue in this case).
> Here's what I've done: I created an odbc connect to the database,
> opened a cursor, executed the sql, then set a variable: namelist =
> cursor.fetchall().  What's got me stumped is that "namelist" is a list
> of tuples with sample data like: [('smith',), ('johnson',),
> ('jackson',), ('jones',)]
> If this were a normal tuple or list, I could easily use:
> "if empname in namelist:
>      ....execute code...."
> But with the list of tuples, I don't know how to easily do this. My
> hunch is that there must be some way to convert that list of tuples
> into a list of single values, since I only need the first element of
> the tuple like 'jackson' from ('jackson',).  Once I had the list of
> single values, my if statement above would work.
> Thanks for your help!
> Jeff

More information about the Python-list mailing list