How to shift list indexes by a certain value in Python
<p>I need to create a python function that right shifts values in a list by a given value. </p>

<p>For example if the list is [1,2,3,4] and the shift is 2 it will become [2,3,4,1]. the shift value must be a non negative integer. I can only use the len and range functions. </p>

<p>This is what I have so far</p>

<pre><code>def shift(array, value):
    if value &lt; 0:
        return
    for i in range(len(array)): 
        arr[i] = arr[(i + shift_amount) % len(arr)]
</code></pre>

<p>I make some modifications in your code (If you have to use <code>len</code> and <code>range</code> functions) :</p>

<pre><code>def shift(array, shift_amount):
    if shift_amount &lt; 0:
        return
    ans = []
    for i in range(len(array)): 
        ans.append(array[(i + shift_amount) % len(array)])
    print ans
shift([1,2,3,4],2)
</code></pre>

<p>Output:</p>

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

<blockquote>
  <p><strong>Note:</strong> </p>
  
  <ul>
  <li>Your's logic is correct but your overriding values in same array, So    I created another list and append value to it.</li>
  <li>If shift value is 1 then output will be <code>[2, 3, 4, 1]</code>. So for value 2    it will be two shifts that's why output should be <code>[3, 4,
  1, 2]</code></li>
  <li>value and shift_amount are two different variables in your code, So I    use only single variable.</li>
  </ul>
</blockquote>

<p>You can use <em>list comprehension</em> (If you want to check in detail about <em>list comprehension</em> see this article <a href="http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/" rel="nofollow">Python List Comprehensions: Explained Visually</a>) like </p>

<pre><code>def shift(array, shift_amount):
    if shift_amount &lt; 0:
        return
    length = len(array)
    print [array[(i + shift_amount) % length] for i in range(length)]
shift([1,2,3,4],0)
</code></pre>

python indexing