Comparing strings in a list
<p>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.</p>

<p>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.</p>

<p>So far, I have the following (since all entry2 dictionaries only have 2 list elements within 'affiliation'):</p>

<pre><code>entry3 = [s for s in entry2 if s['affiliation'][0]['affilname'] != s['affiliation'][1]['affilname']]
</code></pre>

<p>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:</p>

<pre><code>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']]
</code></pre>

<p>Can anyone help me with what is going on?</p>

<p>Thanks</p>

<p>The <em>filter</em> condition of your <em>list comprehension</em> is not properly structured. <code>any</code> returns a boolean which you're comparing with the <code>affilname</code> entry - a string. That would return all the entries since a string will never be equal to a boolean.</p>

<p>You can instead check if there is any entry with <code>affilname</code> subdict that is not the matching the first <code>affilname</code> in that category/sub-dict level:</p>

<pre><code>entry3 = [s for s in entry2 if any(dct['affilname'] != s['affiliation'][0]['affilname'] for dct in s['affiliation'])]
</code></pre>

<p>Once there is a mismatch at that subdict level, any <em>breaks</em> and returns <code>True</code>, which will add that entry to <code>entry3</code></p>

python python-3.5