I have a function for filtering a list of dict based on the value of certain keys, like a SELECT * WHERE xxx query in SQL
list_of_dict = [
{'key1':val, 'key2':val},
{'key1':val, 'key2':val},
...
]
def filter_list(list_of_dict, key1, key2=None):
if key2:
filtered_list = [i for i in list_of_dict if i['key1']==key1 and i['key2']==key2]
else:
filtered_list = [i for i in list_of_dict if i['key1']==key1]
but when I have more keys as arguments to the function the if ...else... could go really long.
Is there a more pythonic way to do this?