Reputation: 353
I'm trying to filter out search results from an API by trying to find and exclude dictionary entries which have 'affiliation names' which are all the same.
To cut a long story short, in the code below, entry2 is a list of 20 dictionaries all of which have nested dictionaries within them, one of which is 'affiliation'. Within this nested dictionary 'affiliation' for each element of entry2, I want to compare the 'affilnames' and if they are not all equal pass the entry2 dictionary element in question to a new list, entry3.
So far, I have the following (since all entry2 dictionaries only have 2 list elements within 'affiliation'):
entry3 = [s for s in entry2 if s['affiliation'][0]['affilname'] != s['affiliation'][1]['affilname']]
which works fine (and returns entry3 having 9 dictionary entries). However, it may not always be the case that there are only 2 list entries within 'affiliation' and so I want to find a way to compare all of the strings within 'affiliation'. I have the following line of code which logically makes sense to me but is returning entry3 as having the same number of dictionary elements as entry2:
entry3 = [s for s in entry2 if any(s['affiliation'][i]['affilname'] for i in range(1,len(s['affiliation'])-1)) != s['affiliation'][0]['affilname']]
Can anyone help me with what is going on?
Thanks
Upvotes: 1
Views: 81
Reputation: 78554
The filter condition of your list comprehension is not properly structured. any
returns a boolean which you're comparing with the affilname
entry - a string. That would return all the entries since a string will never be equal to a boolean.
You can instead check if there is any entry with affilname
subdict that is not the matching the first affilname
in that category/sub-dict level:
entry3 = [s for s in entry2 if any(dct['affilname'] != s['affiliation'][0]['affilname'] for dct in s['affiliation'])]
Once there is a mismatch at that subdict level, any breaks and returns True
, which will add that entry to entry3
Upvotes: 2