snipt

Ctrl+h for KB shortcuts

Python 3

allin() and anyin() as alternatives to `in` comparator

1
2
3
4
5
6
def allin(what, where):
    return all(x in where for x in what)


def anyin(what, where):
    return any(x in where for x in what)
https://snippets.siftie.com/embed/2189542c9ab35c1fbe8618a1f2eb0dc7/
/raw/2189542c9ab35c1fbe8618a1f2eb0dc7/
2189542c9ab35c1fbe8618a1f2eb0dc7
python3
Python 3
6
2019-04-18T20:47:21
True
False
False
Oct 16, 2012 at 10:31 AM
/api/public/snipt/39631/
allin-and-anyin-as-alternatives-to-in-comparator
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="k">def</span> <span class="nf">allin</span><span class="p">(</span><span class="n">what</span><span class="p">,</span> <span class="n">where</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="ow">in</span> <span class="n">where</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">what</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="k">def</span> <span class="nf">anyin</span><span class="p">(</span><span class="n">what</span><span class="p">,</span> <span class="n">where</span><span class="p">):</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">return</span> <span class="nb">any</span><span class="p">(</span><span class="n">x</span> <span class="ow">in</span> <span class="n">where</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">what</span><span class="p">)</span> </span></pre></div> </td></tr></table>
function, python, util