I have a list of lists containing dictionaries:
[[{'event_id': 1, 'order_id': 1, 'item_id': 1, 'count': 1, 'return_count': 0, 'status': 'OK'},
{'event_id': 2, 'order_id': 1, 'item_id': 1, 'count': 1, 'return_count': 0, 'status': 'OK'}],
[{'order_id': 2, 'item_id': 1, 'event_id': 1, 'count': 3, 'return_count': 1, 'status': 'OK'},
{'order_id': 2, 'event_id': 2, 'item_id': 1, 'count': 3, 'return_count': 1, 'status': 'OK'},
{'order_id': 2, 'event_id': 1, 'item_id': 2, 'count': 4, 'return_count': 2, 'status': 'OK'}]]
For each item in the given order I only need those dictionaries whose event_id
is max
. So I wrote the following code:
for el in lst:
for element in el:
if element['event_id'] != max(x['event_id'] for x in el if element['item_id'] == x['item_id']):
el.remove(element)
lst
is the initial list.
For some reason, after running the code lst
remains unchanged.