modify lists removing elements without making a mess
<p>I'm trying to resolve now a task that sounds like that:</p>

<blockquote>
  <p>''write a function modi(la, lb) that takes in input 2 lists la and lb that have the same number of elements inside. The function should <strong>modify</strong> lists la and lb camparing elements with the same indexes in two lists and deleting a bigger one, if elements are equal function delete both of them. ''</p>
</blockquote>

<p>For example:</p>

<ul>
<li>la = ['bear', 'tiger', 'wolf', 'whale', 'elephant'] </li>
<li>lb = ['swan', 'cat', 'dog', 'duck', 'rabbit']</li>
</ul>

<p>So the functin should return:
['bear','elephant']
['cat','dog','duck']</p>

<p>I have wrote the next code but it doesn't modify lists but create new ones and add there elements. Some ideas how can i do that?</p>

<pre><code>def confront(s1, s2):   

    if s1 &lt; s2:   # condition that tells which element to choose later
        return 0
    elif s2 &lt; s1:
        return 1
    else:
        return 2


def modi(la,lb):

    latemp = []

    lbtemp = []

    i = 0

    while i &lt; len(la):


        q = confront(la[i], lb[i])
        if q == 0:
            latemp.append(la[i])

        elif q == 1:
            lbtemp.append(lb[i])


        i +=1  
    la = latemp
    lb = lbtemp
    return la, lb
</code></pre>

<p>I have tried remove() but it have created a big mess</p>

<p>In your own code <code>la = latemp</code> and <code>lb = lbtemp</code> create <em>references</em> to the two new lists that are local to the function, they have no bearing on the lists that you pass in to the function. According to your specs, you should be mutating the lists passed in not reassigning or creating new lists.</p>

<p>Since you are doing an elementwise comparison, you could use <em>zip</em> and remove from each list according to your specs:</p>

<pre><code>def modi(la, lb):
    for i, j in zip(la, lb):
        # both the same
        if i == j:
            la.remove(i) 
            lb.remove(i)
        # element from la is bigger
        elif i &gt; j:
            la.remove(i)
        # else element from lb must be bigger
        else:
            lb.remove(j)


la = ['bear', 'tiger', 'wolf', 'whale', 'elephant']
lb = ['swan', 'cat', 'dog', 'duck', 'rabbit']

modi(la, lb) # will modify the lists passed in.

print(la, lb) 
</code></pre>

python list python-3.x