The code is completely equivalent to the one you showed:
def get_query_results(*filters): res = models.Item.query for i, filt in enumerate(filters, 1): if filt is not None: d = {'filter{}'.format(i): filt} res = res.filter(**d) return res.all()
I'm not quite sure why you need the named argument res.filter be specifically filter1 , filter2 , etc., but this snippet will do this without a repeating pattern that you, for obvious reasons, want to avoid.
If the names are not actually filter1 , filter2 , etc., this is normal as long as the required names are known:
NAMES = 'foo bar baz bat'.split() def get_query_results(*filters): res = models.Item.query for name, filt in zip(NAMES, filters): if filt is not None: d = {name: filt} res = res.filter(**d) return res.all()
This option will work in this case.
source share