SQLite IN operator with multiple conditions in for loop

27 Views Asked by At

I have the following:

x = ['dat1','dat2']
y = ['M','F']

for x,y in zip(x,y):
    conn.execute('''
                create table %s as
                select sex as sex
                from raw
                where sex in (?)
                '''%(x),(y )
                )

conn.commit()

How do set the for loop/list to capture both M and F strings along with M and F individually without resorting to joins? I've tried various lists such as this y = ['M','F','"M","F"'] but they don't work (I included 'dat3' in x as well). Is format() needed?

1

There are 1 best solutions below

1
bagelanta On

It can be done with the LIKE operator:

x = ['dat1','dat2','dat3']
y = ['M%','F%','%']

for x,y in zip(x,y):
    conn.execute('''
                create table %s as
                select sex as sex
                from raw
                where sex like ?
                '''%(x),(y, )
                )

conn.commit()