While the suggested *fixes* are all correct (when in doubt, explicilty parenthesize!), none of the *explanations* for the unexpected output are quite right.  Since this involves a subtle issue, I thought I&#39;d send a full explanation.  <br>
<br>It&#39;s tempting to assume that <span style="font-family: courier new,monospace;"><br>
<br>
&gt;&gt;&gt; a in alist == b in alist</span><br>
is equivalent to <br>
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; ((a in alist) == b) in alist</span><br>
<br>
However, this is not correct!<br>
<br>
For a simpler (but perhaps more confusing) example of the hazards of
using &quot;in&quot; and &quot;==&quot;, non-parenthesized, in an expression like this,
consider the following:<br>
<br><span style="font-family: courier new,monospace;">&gt;&gt;&gt; a = 1 # as before</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&gt;&gt;&gt; alist = [5,6] # as before</span><br>

<span style="font-family: courier new,monospace;">&gt;&gt;&gt; a in alist == False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; (a in alist) == False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True<br>
</span><br><span style="font-family: courier new,monospace;"></span>Weird, right?  And no, putting parens around (alist == False) won&#39;t work--that would be an exception because the right side of the &#39;in&#39; operator wouldn&#39;t be iterable.<br>

<br>
Here&#39;s one last example:<br>
<br>
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; blist = [1, [5,6]]</span><br>
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; 5 in alist == [5,6] in blist</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True</span><br>
<br>
You might enjoy the exercise of figuring out why the above output is correct.  But to cut to the chase, what&#39;s going on is the following.  Python allows comparisons to be chained, as in the following:<br>
<br>
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; a == 1 == 2/2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; 1 &lt; 5 &lt; 7</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">True</span><br>
<br>
The way the chaining works (see 5.9 in <a href="http://tinyurl.com/3vsb6m">http://tinyurl.com/3vsb6m</a>) is that<br>
<br>
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; a == 1 == 2/2<br>
<span style="font-family: arial,helvetica,sans-serif;">is equivalent to </span><br>
</span><span style="font-family: courier new,monospace;">&gt;&gt;&gt; (a == 1) and (1 == 2/2)</span><span style="font-family: courier new,monospace;"><br>
<span style="font-family: arial,helvetica,sans-serif;"><br>
and <br>
<br>
</span></span><span style="font-family: courier new,monospace;">&gt;&gt;&gt; 1 &lt; 5 &lt; 7<br>
<span style="font-family: arial,helvetica,sans-serif;">is equivalent to</span><br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">&gt;&gt;&gt; (1 &lt; 5) and (5 &lt; 7)</span><span style="font-family: courier new,monospace;"></span><br>
<br>
Since &#39;in&#39; is just another comparison operator, it works the same way.  Thus, the the original expression<br>
<br>
<span style="font-family: courier new,monospace;">&gt;&gt;&gt; a in alist == b in alist</span><br>
is equivalent to <br>
&gt;&gt;&gt;<span style="font-family: courier new,monospace;">(a in alist) and (alist == b) and (b in alist)</span><br>
<br>
which is <span style="font-family: courier new,monospace;">False</span> because all three comparisons are <span style="font-family: courier new,monospace;">False</span>.  You&#39;ll see that the two other examples I came up with make sense in this context as well.<br>

<br>
-Damon<br><br><br><br><div class="gmail_quote">On Mon, Jan 25, 2010 at 1:27 PM, Asher Langton <span dir="ltr">&lt;<a href="mailto:langton2@llnl.gov">langton2@llnl.gov</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Jan 25, 2010, at 1:12 PM, Max Slimmer wrote:<br>
</div><div><div></div><div class="h5"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Can anyone explain the following:<br>
<br>
&gt;&gt;&gt; a = 1<br>
&gt;&gt;&gt; b = 2<br>
&gt;&gt;&gt; alist = [5,6]<br>
&gt;&gt;&gt; print a in alist<br>
False<br>
<br>
&gt;&gt;&gt; a in alist == b in alist<br>
False<br>
&gt;&gt;&gt; a in alist == a in alist<br>
False<br>
&gt;&gt;&gt; bool(a in alist) == bool(b in alist)      # this does what we expect<br>
True<br>
&gt;&gt;&gt; c = 5<br>
&gt;&gt;&gt; c in alist == c in alist<br>
False<br>
&gt;&gt;&gt;<br>
</blockquote>
<br></div></div>
The &#39;==&#39; and &#39;in&#39; operators have the same precedence, so the expression &#39;a in alist == b in alist&#39; is evaluated left-to-right as:<br>
<br>
&gt;&gt;&gt; ( (a in alist) == b) in alist<br>
<br>
Since &#39;a in alist&#39; is False, this is the same as<br>
<br>
&gt;&gt;&gt; ( False == b) in alist<br>
<br>
which can be simplified to<br>
<br>
&gt;&gt;&gt; False in alist<br>
<br>
which is False.<br><font color="#888888">
<br>
<br>
-Asher</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>