title: Python GeeksForGeeks arrange array alternatively problem
body: <p>I am attempting a Python practice problem on GeeksForGeeks, but my code raises IndexError every time.</p>
  <p>The problem is as follows:</p>
  <blockquote>
  <p>Given a sorted array of positive integers <code>arr</code>, and an integer <code>n</code> which represents the length of <code>arr</code>, your task is to rearrange the array elements alternatively i.e first element should be max value, second should be min value, third should be second max, fourth should be second min and so on.
  Note: Modify the original array itself.</p>
  </blockquote>
  <p>My code however raises <code>IndexError: list index out of range</code></p>
  <p>Here is my code:</p>
  <pre><code>def rearrange(arr, n):
          arr1,arr2 = sorted(arr[:(n//2)]), sorted(arr[(n//2):],reverse=True)
          arr.clear()
          for i in range(n):
              arr.append(arr2[0]); arr2.pop(0)
              arr.append(arr1[0]); arr1.pop(0)
  </code></pre>
  <p>Here is the error:</p>
  <pre><code>File &amp;quot;/home/4c7d0350d40e21b84be527508f21cb47.py&amp;quot;, line 9, in rearrange
      arr.append(arr2[0]); arr2.pop(0)
  IndexError: list index out of range
  </code></pre>
  <p>I don't understand why this is happening. Can anyone provide an explanation?</p>