Jun-09-2021, 09:09 AM
In this thread from just a couple of days ago here, an elegant solution was given.
I was impressed, so I am trying to figure out how it works. Something similar may be useful for me.
But I cannot see how the if clause does its job.
Could some kind soul please enlighten me?
I tried taking away the if clause, then filter_dicts(data, *args, **kwargs) returns id and result from all the dictionaries in the list.
I added some bits to the solution given to make it more human-readable for me.
I was impressed, so I am trying to figure out how it works. Something similar may be useful for me.
But I cannot see how the if clause does its job.
Could some kind soul please enlighten me?
I tried taking away the if clause, then filter_dicts(data, *args, **kwargs) returns id and result from all the dictionaries in the list.
I added some bits to the solution given to make it more human-readable for me.
lexis = [{"id": 1, "a": 1, "b": 2, "result": 9.82}, {"id": 2, "a": 1, "b": 2, "result": -5}, {"id": 3, "a": 1, "b": 5, "result": 7.98}, {"id": 4, "a": 1, "b": 2, "result": 11.1}, {"id": 5, "a": 1, "b": 2, "result": -4}, {"id": 6, "a": 1, "b": 5, "result": 6.33}, {"id": 7, "a": 1, "b": 2, "result": 8.88}, {"id": 8, "a": 1, "b": 2, "result": -2}, {"id": 9, "a": 1, "b": 5, "result": 5.44}, {"id": 10, "a": 1, "b": 5, "result": 10}] def filter_dicts(data, *args, **kwargs): # added by me for a in args: print('args are:', a) # returns id and result for k in kwargs: print('kwargs are:', k) # returns a and b for row in data: print('data contains:', row) if row.items() >= kwargs.items(): # row.items() is 4 tuples long, kwargs.items() is 2 tuples long print('row.items are:', row.items()) print('kwargs.items are:', kwargs.items()) # end of added by me return ({key: row[key] for key in args} for row in data if row.items() >= kwargs.items()) res = filter_dicts(lexis, 'id', 'result', a=1, b=2) for r in res: print(r)