How to recode line so an exact sentence must be in the list for it to match
<pre><code>x = ["Cookie flavored water is yummy 6", "Coding complicated 16", "Help 7"]

for i in x:
    if "flavored" in x:
        print ("Yes")
    else:
        print ("No")
</code></pre>

<p>I want the exact string "Cookie flavored water is yummy" to be in the list for it to be acceptable but I don't want the 6 part included. I'm completely befuddled on how to accomplish this. Also the objective might change from the first element to a different element.</p>

<p>you're iterating on <code>x</code> with <code>i</code> but you check if string belongs to the list, not the element, which is always false.</p>

<p>to check if an element of x contains <code>"Cookie flavored water is yummy"</code></p>

<pre><code>x = ["Cookie flavored water is yummy 6", "Coding complicated 16", "Help 7"]

for i in x:
    print ("Yes" if "Cookie flavored water is yummy" in i else "No")
</code></pre>

<p>on the other hand, for exact string match simply use <code>in</code> without a loop, the loop being made on <code>x</code> by the <code>in</code> operator:</p>

<pre><code>print ("Yes" if "Cookie flavored water is yummy" in x else "No")
</code></pre>

<p>If you need exact string match on a great number of elements, consider putting your elements in a <code>set</code> instead because lookup time is much smaller (hashing involved). The code remains the same apart from that.</p>

python list