Python slice command forgotten out of the loop
<p>I want to build some lists (a, b, c, d) by extending periodically those given in input.</p>

<p>The structure of the periodicity is such that the first element of each list must <em>not</em> be repeated, while all the other elements must be until the maximum length set in input is reached (it is possible that the period does not get to be repeated an integer number of times).</p>

<p>To give an example, if I have  as input</p>

<pre><code>a = [1, 2, 3, 4]          max_len=11
</code></pre>

<p>I want to obtain as output</p>

<pre><code>a = [1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2]
</code></pre>

<p>I wrote this piece of code:</p>

<pre><code>for mylist in [a, b, c d]:   
    period = mylist[1:]     # all elements but the first are repeated
    while len(mylist)&lt; max_len: 
        mylist.extend(period)
    mylist = mylist[:max_len]   # cut to max_len                        
    print mylist
print a, b, c, d
</code></pre>

<p>If I run this, I see from the two print commands that my lists are how I want them to be while the program is still in the loop, but they get back to the "non-sliced" length when out of the loop, that is, a gets back to the length greater than max_len where the period is repeated exactly 4 times:</p>

<pre><code> a = [1, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4]
</code></pre>

<p>Why is that? It looks like the program has forgotten the slicing (but not the extension).</p>

<p>I understand that it is a pretty simple problem, and I could get around it in some other way, but I would like to understand why this idea does not work.
Thank you for your help!</p>
