query = 'SELECT * FROM fruits'
params = []
whereclauses = []
for filtername in ui_filters:
    filtervalue = obtain_filter_value_for(filtername)
if filtervalue != 'All':
    whereclauses.append('{} = ?'.format(filtername))
params.append(filtervalue)
if whereclauses:
    query = '{} WHERE {}'.format(query, ' AND '.join(whereclauses))
cursor.execute(query, params)
How to implement efficient filtering logic in Python?