Repeat list if index range is out of bounds
<p>I have a Python list</p>



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

<p>and I'd like to get a range of indices such that if I select the indices <code>0</code> through <code>N</code>, I'm getting (for <code>N=10</code>) the repeated</p>



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

<p>I could of course repeat the list via <code>(int(float(N) / len(a) - 0.5) + 1) * a</code> first and select the range <code>[0:10]</code> out of that, but that feels rather clumsy.</p>

<p>Any hints?</p>

<p>You can simply use the modulo operator when accessing the list, i.e.</p>

<pre><code>a[i % len(a)]
</code></pre>

<p>This will give you the same result, but doesn't require to actually store the redundant elements.</p>

python