IndexError: " pop index out of range" with a for loop
<p>Goodmorning,
I just wrote this program in python and this <code>IndexError</code> keeps showing up. I don't know how to resolve it, I even tried by using a while loop but nothing changed...I hope someone can help me with this problem!</p>

<p>Here is my code, it should check the length of the object of two lists (la, lb) and: remove the string from the la list  if the string is less long than the lb string and vice versa..plus it has to remove both of the strings if their length is the same.</p>

<pre><code>def change(l1,l2):
    la1 = l1[:]
    la2 = l2[:]
    i = 0
    for i in range(len(la1)): 
        if la1[i] == la2[i]:
            l1.pop(i)
            l2.pop(i)
        elif la1[i] &gt; la2[i]:
            l2.pop(i)
        elif la2[i] &gt; la1[i]:
            l1.pop(i)
</code></pre>

<p>You can traverse the lists backwards, so that when you remove an item from the list the indices of the elements that you have not examined yet won't be affected</p>

<pre><code>def f(a, b):
    l = len(a) if len(a)&lt;len(b) else len(b)
    for i in range(l):
        j = l-i-1
        la, lb = len(a[j]), len(b[j])
        if la&lt;lb: a.pop(j)
        elif lb&lt;la: b.pop(j)
        else: a.pop(j), b.pop(j)
    return a, b
</code></pre>

<p>ps I staid faithful to your problem statement and not to your implementation re the comparison based on strings' lengths.</p>

python