Python how cyclic fetch a pre-fixed number of elements in array
<p>I'm trying to make a function that will always return me a pre-fixed number of elements from an array which will be larger than the pre-fixed number:</p>

<pre><code>def getElements(i,arr,size=10):
    return cyclic array return
</code></pre>

<p>where <code>i</code> stands for index of array to fetch and <code>arr</code> represent the array of all elements:</p>

<h2>Example:</h2>

<pre><code>a = [0,1,2,3,4,5,6,7,8,9,10,11]
b = getElements(9,a)
&gt;&gt; b
&gt;&gt; [9,10,11,0,1,2,3,4,5,6]
b = getElements(1,a)
&gt;&gt; b
&gt;&gt; [1,2,3,4,5,6,7,8,9,10]
</code></pre>

<p>where <code>i = 9</code> and the array return the <code>[9:11]+[0:7]</code> to complete <strong>10 elements</strong> with <code>i = 1</code> don't need to cyclic the array just get <code>[1:11]</code></p>

<p>thanks for the help</p>

<h2>Initial code (not working):</h2>

<pre><code>def getElements(i,arr,size=10):
    total = len(arr)
    start = i%total
    end = start+size
    return arr[start:end]

#not working cos not being cyclic
</code></pre>

<h2>EDIT:</h2>

<p>I can't make any  <code>import</code> for this script</p>

<pre><code>def get_elements(i, arr, size=10):
    if size - (len(arr) - i) &lt; 0:
        return arr[i:size+i]
    return arr[i:] + arr[:size - (len(arr) - i)]
</code></pre>

<p>Is that what you want? <strong>Updated to work with lower numbers.</strong></p>

python arrays circular-list