Removing duplicates from list of dicts is as simple as set(list) but i have got stuck with this specific task.
I need to filter a list of dicts only by some keys and keep only the first elements
flights = [ { 'dep_code': 'LON', 'arr_code': 'PAR', 'airline': 'BA', 'date': '2022-07-07' }, { 'dep_code': 'LON', 'arr_code': 'PAR', 'airline': 'BA', 'date': '2022-08-07' }, { 'dep_code': 'LON', 'arr_code': 'PAR', 'airline': 'BA', 'date': '2022-09-07' }, { 'dep_code': 'LON', 'arr_code': 'BRU', 'airline': 'BA', 'date': '2022-10-07' }, { 'dep_code': 'LON', 'arr_code': 'BRU', 'airline': 'BA', 'date': '2022-11-07' }, ]
I need to get this very result (keep only first elements with same ‘dep_code’ and ‘arr_code’)
flights = [ { 'dep_code': 'LON', 'arr_code': 'PAR', 'airline': 'BA', 'date': '2022-07-07' }, { 'dep_code': 'LON', 'arr_code': 'BRU', 'airline': 'BA', 'date': '2022-10-07' }, ]
Thanks for your time!
Advertisement
Answer
You need to save all found data in a list.
found = list() result = list() for d in flights: if not [d['dep_code'],d['arr_code']] in found: result.append(d) found.append([d['dep_code'],d['arr_code']]) print(result)