Removing matching items from two lists of dicts

Viewed 1233

I need to take two dictionaries and filter out the 'garbage' items that are unrecognized names:

data = [
    {'annotation_id': 22, 'record_id': 5, 'name': 'Joe Young'},
    {'annotation_id': 13, 'record_id': 7, 'name': '----'},
    {'annotation_id': 12, 'record_id': 9, 'name': 'Greg Band'},
]

garbage = [
    {'annotation_id': 13, 'record_id': 7, 'name': '----'}
]

So in this case I need to remove annotation_id 13 from data.

I tried iterating over the list and removing it but I understand that does not work well in python. I also tried a list comprehension but am failing with that as well. What I am doing wrong? My code is below:

data = [[item for item in data if item['name'] != g['name'] for g in garbage]

The above code creates many duplicate versions of the dicts.

3 Answers
Related