Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23390

How do I filter a list of dicts with a list of strings in Python? [duplicate]

$
0
0

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 = ['Bo', 'Alic']# 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. Additionally, I am trying to see if list2 is contained as a substring of the list_of_dicts['name']

Cell In[8], line 1, in <lambda>(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

Viewing all articles
Browse latest Browse all 23390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>