I have a list of dicts which I would like to filter with another list, but am having trouble with the filter()
function
list_of_dicts = [{'name': 'Bob'},{'name': 'Alice'},{'name':'Jenny'}]
list_2 = ['Bob', 'Alice']
# filter(lambda name: any(list2) in list_of_dicts['name'], list_of_dicts)
Ideally, I would want above to return:
[{'name': 'Bob'},{'name': 'Alice'}]
or the converse
# filter(lambda name: any(list_2) not in list_of_dicts['name'], list_of_dicts)
[{'name':'Jenny'}]
The requirement here is that I use the filter() function
`
Cell In[8], line 1, in (name)
—-> 1 list(filter(lambda name: any(list_2) in list_of_dicts[‘name’], list_of_dicts))
TypeError: list indices must be integers or slices, not str
`
1