Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code <em>already</em> creates a new one each time through the loop.</p> <pre><code>candidateArray = findLowestPrices(...) </code></pre> <p>This assigns a new list to the variable, <code>candidateArray</code>. It should work fine.</p> <p>When you do this:</p> <pre><code>del candidateArray[:] </code></pre> <p>...you're deleting the contents of the <em>same list</em> you just appended to the master list.</p> <p>Don't think about pointers or variables; just think about objects, and remember nothing in Python is ever implicitly copied. A list is an object. At the end of the loop, <code>candidateArray</code> names the same list object as <code>multiCandidateArray[-1]</code>. They're different <em>names</em> for the same thing. On the next run through the loop, <code>candidateArray</code> becomes a name for a <em>new</em> list as produced by <code>findLowestPrices</code>, and the list at the end of the master list is unaffected.</p> <p>I've <a href="http://me.veekun.com/blog/2012/05/23/python-faq-passing/" rel="nofollow">written about this before</a>; the C way of thinking about variables as being predetermined blocks of memory just doesn't apply to Python at all. Names are moved onto values, rather than values being copied into some fixed number of buckets.</p> <p>(Also, nitpicking, but Python code generally uses <code>under_scores</code> and doesn't bother with types in names unless it's really ambiguous. So you might have <code>candidates</code> and <code>multi_candidates</code>. Definitely don't call anything an "array", since there's an <code>array</code> module in the standard library that does something different and generally not too useful. :))</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