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?
Calling print (del_same_elements([3,11],[2,2])) returns [11],[2,2] as expectedHow it is possible? There is no common elements in both lists?[2]and[2,2], do you expect to get two empty lists out, or should the second one still have one2? Also, is it desirable (or especially undesirable) for the list modifications to happen in place?