How to keep track of whether all strings in a list have a specific string present in them?
<p>I have a list of strings within which I need to search for a specific sub-string and return a result only when the sub-string is present in every string that is in the list. My first idea was to create a list of boolean values that correspond with whether the sub-string is present in each larger string in my list, and then write another loop to check whether this list of booleans is True at every index. Does anybody have any suggestions for a more elegant and efficient method?</p>

<p>Thank you kindly!</p>

<p>you can use <a href="https://docs.python.org/2/library/functions.html#all" rel="nofollow"><code>all()</code></a> and <a href="https://docs.python.org/2/reference/expressions.html#in" rel="nofollow"><code>in</code></a>:</p>

<pre><code>l = ["pie", "lie", "die"]
all( ['ie' in x for x in l] )
</code></pre>

<p>The above version may perform <a href="http://cis.poly.edu/cs1114/pyLecturettes/short-circuit-eval.html" rel="nofollow">unnecessary checks</a>.</p>

<pre><code>all( 'ie' in x for x in l )
</code></pre>

python list boolean