0

I am writing a function to delete an element which is the same in both lists.

 def del_same_elements(num1,num2):
        for i in range(0,len(num1)):
            for j in range(0,len(num2)):

                if num1[i] == num2[j]:
                    same = num1[i]

        num1.remove(same)
        num2.remove(same)

        return num1,num2

Calling print (del_same_elements([3,11],[2,2,3])) returns [11],[2,2] as expected, but when trying print (del_same_elements([3],[2,2])) I get the error local variable 'same' referenced before assignment. How do I handle the case where there are no same values?

5
  • 2
    Calling print (del_same_elements([3,11],[2,2])) returns [11],[2,2] as expected How it is possible? There is no common elements in both lists? Commented Apr 15, 2015 at 7:39
  • I made a typo! I will edit. Commented Apr 15, 2015 at 7:39
  • 2
    If you have lists [2] and [2,2], do you expect to get two empty lists out, or should the second one still have one 2? Also, is it desirable (or especially undesirable) for the list modifications to happen in place? Commented Apr 15, 2015 at 7:41
  • I would expect [] and [2]. Commented Apr 15, 2015 at 7:44
  • Please describe in detail and with more samples what you are trying to do. Commented Apr 15, 2015 at 7:45

4 Answers 4

3

Using set intersection to detect the common elements:

def rm_same(num1, num2):
    same = set(num1).intersection(num2)
    for s in same:
        num1.remove(s)
        num2.remove(s)
    return num1, num2
Sign up to request clarification or add additional context in comments.

3 Comments

you don't really need the if since looping in an empty list will simply not execute the block.
you don't need to convert the set to a list either :)
Edited to avoid non necessary operation :)
2

Use a bit of set theory and calculate the intersection of the list and then remove the items intersection

def del_same_elements(num1, num2):
    same = list(set(num1) & set(num2))

    for i in same:
        num1.remove(i)
        num2.remove(i)

    return num1, num2

if __name__ == "__main__":
    num1, num2 = del_same_elements([1,2,3,4,5], [1,3,5,6])

print num1
print num2

the result

[2, 4]
[6]

This also worked perfectly where there is not interseciton since nothing will be removed

1 Comment

This is perfect! I never even thought of this.
1

Alternatively, you could have written program like this

def del_same_elements(num1,num2):
    for num in num1 + num2:
        if num in num1 and num in num2:
            num1.remove(num)
            num2.remove(num)

    return num1, num2

print (del_same_elements([3,11],[2,2, 3]))

This can be a better solution.

Comments

0

OR, you could use sets instead:

same = set(num1) & set(num2)

while same:
    val = same.pop()
    num1.remove(val)
    num2.remove(val)

Which at least avoids those nested loops.

1 Comment

Apologies for being slow in producing a set solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.