Check if multiple lists contain multiple items
<p>I have a list of lists in my python code, and I want to check if <em>any</em> of those lists contains a certain two items.</p>

<pre><code>f1=['a','b']
f2=['c','d']
f3=['e','f']
f4=['g','h']
f5=['i','j']
f6=['k','l']

flist=[f1,f2,f3,f4,f5,f6]
</code></pre>

<p>I want something like:</p>

<pre><code>if 'a' in flist[0:5] and 'b' in flist[flist.index('a')]:
   print (true)
</code></pre>

<p>What's the easiest way to do this?</p>

<p>You can use <a href="https://docs.python.org/2/library/functions.html#any" rel="nofollow"><code>any</code></a> with <code>issubset</code>:</p>

<pre><code>if any({'a', 'b'}.issubset(sublist) for sublist in flist):
    print "a and b were found"
</code></pre>

<p>By using <code>any</code>, the search is called off as soon as a <em>superset</em> of the search items is found.</p>

<p>This handles the general case where the sublists may contain more than two items.</p>

<hr>

<p>But if the <em>sublists</em> will always contain two items, it is sufficient to check for equality:</p>

<pre><code>if any({'a', 'b'} == set(sublist) for sublist in flist):
    print "a and b were found"
</code></pre>

python list