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
foo=s['affiliation'], then useany(bar['affilname'] for bar in foo[1:-1])instead ofrange()etc. Then the error will be easy to spot. Of course do not usefooandbarbut choose some meaningful names... :)