Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, you have two lists:</p> <pre><code>&gt;&gt;&gt; a = [1,2,3,4,5] &gt;&gt;&gt; b = [6,7,8,9,0] </code></pre> <p>... and you want to iterate over <code>a and b</code>. So what <em>is</em> <code>a and b</code>, exactly?</p> <pre><code>&gt;&gt;&gt; a and b [6, 7, 8, 9, 0] </code></pre> <p>That might look odd, but it's the result of two facts about Python:</p> <ol> <li><p>Every object is either <code>True</code>-ish or <code>False</code>-ish. For example:</p> <pre><code>&gt;&gt;&gt; bool(a) True &gt;&gt;&gt; bool(b) True </code></pre> <p>In fact, all lists except the empty list <code>[]</code> are <code>True</code>-ish.</p></li> <li><p>Python uses <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation" rel="nofollow">short-circuit evaluation</a>, which means that, for <code>a and b</code>, it:</p> <ul> <li><p>Checks whether <code>a</code> is <code>True</code>-ish or <code>False</code>-ish</p></li> <li><p>If <code>a</code> is <code>False</code>-ish, evaluates to <code>a</code></p></li> <li><p>If <code>a</code> is <code>True</code>-ish, evaluates to <code>b</code></p></li> </ul> <p>Following those rules, you should be able to see why <code>a and b</code> evaluates to <code>[6, 7, 8, 9, 0]</code> in your case (and following the same rules for combinations of the actual values <code>True</code> and <code>False</code> will show you that short-circuit evaluation does make sense).</p></li> </ol> <p>If what you want to actually do is iterate trough the items in <code>a</code> and then those in <code>b</code>, you can just use the <code>+</code> operator to concatenate them:</p> <pre><code>&gt;&gt;&gt; for item in a + b: ... print item, ... 1 2 3 4 5 6 7 8 9 0 </code></pre> <p>As for your followup questions:</p> <blockquote> <p>What might happen with other operators, such as <code>or</code> and <code>not</code>?</p> </blockquote> <p><code>or</code>'s rules for short-circuit evaluation are different (you can look them up for yourself or just follow the link above), and in your case <code>a or b</code> evaluates to <code>[1, 2, 3, 4, 5]</code> (in other words, <code>a</code>).</p> <p><code>not</code> always returns <code>True</code> for a <code>False</code>-ish value and <code>False</code> for a <code>True</code>-ish value, and since you can't iterate over <code>True</code> or <code>False</code>, you'll get a <code>TypeError</code>.</p> <blockquote> <p>Is this proper usage, even? If so, is it messy, unsafe, or frowned upon?</p> </blockquote> <p>Well, there's nothing illegal about it, but as you can see, it doesn't do what you want. There are circumstances where (ab)using short-circuit evaluation to choose an iterable over which to iterate might be helpful, but this isn't one of them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload