Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You <strong>appended</strong> <code>bList</code> to <code>aList</code>, then popped it off again and appended <code>aList</code> to <code>bList</code>.</p> <p>Here is what happens step by step:</p> <ol> <li><p><code>aList.append(bList)</code> adds <code>bList</code> as a single value to <code>aList</code>; <code>aList</code> is now:</p> <pre><code>&gt;&gt;&gt; aList=[1,3,"Hello","Barney"] &gt;&gt;&gt; bList=[5,5,5] &gt;&gt;&gt; aList.append(bList) &gt;&gt;&gt; aList [1, 3, 'Hello', 'Barney', [5, 5, 5]] </code></pre> <p>note the <em>nested</em> list; <code>list.append()</code> adds the argument as a single entry in the target list.</p></li> <li><p>You then test if <code>5</code> is in <code>aList</code>; it is not, it is in a <em>nested</em> list:</p> <pre><code>&gt;&gt;&gt; 5 in aList False &gt;&gt;&gt; 5 in aList[-1] True </code></pre></li> <li><p>The <code>else</code> branch uses <code>list.pop()</code> to remove the last element, which is a whole nested list, and appends <code>aList</code> to it; <code>bList</code> still refers to that last list:</p> <pre><code>&gt;&gt;&gt; temp = aList.pop() &gt;&gt;&gt; temp [5, 5, 5] &gt;&gt;&gt; temp is bList True &gt;&gt;&gt; temp.append(aList) &gt;&gt;&gt; bList [5, 5, 5, [1, 3, 'Hello', 'Barney']] </code></pre></li> </ol> <p>You probably wanted to <strong>extend</strong> <code>aList</code> instead, adding just the <em>elements</em> of <code>bList</code> to <code>aList</code>:</p> <pre><code>&gt;&gt;&gt; aList=[1,3,"Hello","Barney"] &gt;&gt;&gt; bList=[5,5,5] &gt;&gt;&gt; aList.extend(bList) &gt;&gt;&gt; aList [1, 3, 'Hello', 'Barney', 5, 5, 5] </code></pre> <p>Now <code>5 in aList</code> is <code>True</code>, and <code>bList</code> is not going to be affected.</p>
 

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