title: list index out of range in a merge and sort function
body: <p>I tried writing a simple merge and sort function in python and got stuck after getting the following error-</p>
  <pre><code>List out of range. 
  </code></pre>
  <p>I would appreciate if you could help me fix it and figure out how to avoid it. I have added the code below-</p>
  <pre><code>def merge(lst1, lst2):
      # Gets two sorted lists and returns one merged and sorted list
      merge_sorted = []
      i = 0
      j = 0
      len1 = len(lst1) - 1
      len2 = len(lst2) - 1
      while i &lt; len1 or j &lt; len2:
          if lst1[i] &lt; lst2[j]:
              merge_sorted.append(lst1[i])
              i += 1
          elif lst1[i] &gt; lst2[j]:
              merge_sorted.append(lst2[j])
              j += 1
          else:
              merge_sorted.append(lst1[i])
              merge_sorted.append(lst2[j])
              i += 1
              j += 1
      return merge_sorted
  
  lst1 = [2, 4, 5, 6, 8]
  lst2 = [1, 3, 7, 9, 0]
  merge(lst1, lst2)
  </code></pre>
  <p>What I got:</p>
  <pre><code>IndexError                                Traceback (most recent call last)
  &lt;ipython-input-13-572aad47097b&gt; in &lt;module&gt;()
       22 lst1 = [2, 4, 5, 6, 8]
       23 lst2 = [1, 3, 7, 9, 0]
  ---&gt; 24 merge(lst1, lst2)
  
  &lt;ipython-input-13-572aad47097b&gt; in merge(lst1, lst2)
        7     len2 = len(lst2) - 1
        8     while i &lt; len1 or j &lt; len2:
  ----&gt; 9         if lst1[i] &lt; lst2[j]:
       10             merge_sorted.append(lst1[i])
       11             i += 1
  
  IndexError: list index out of range
  </code></pre>