1

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

1
  • Readability!!! As long as you write unreadable code, you will keep having problems... This line deserves splitting into at least three separate lines. Cache foo=s['affiliation'], then use any(bar['affilname'] for bar in foo[1:-1]) instead of range() etc. Then the error will be easy to spot. Of course do not use foo and bar but choose some meaningful names... :) Commented Sep 27, 2016 at 12:34

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

@RThompson I made a fix on the indexing. Pls take note

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.