need to compare lists elements to each other and if the are the same, remove them.
list_1 = [1,2,3,4]
list_2 = [1,2,5,3]
>>>>>>
list_1 = [3,4]
list_2 = [5,3]
CodePudding user response:
Try:
list_1 = [1, 2, 3, 4]
list_2 = [1, 2, 5, 3]
list_1[:], list_2[:] = zip(*((a, b) for a, b in zip(list_1, list_2) if a != b))
print(list_1)
print(list_2)
Prints:
[3, 4]
[5, 3]
CodePudding user response:
For this you will have to use nested loops, in this case nested for loops with an if statement. for example
list_1 = [1,2,3,4]
list_2 = [3,4]
for j in list_2:
for i in list_1:
if i == j:
list_1.remove(i)
print(list_1)
This prints the output [1,2]
This method runs through every number one by one and compares it to the numbers in the other list, if it finds a match it removes it from the main list.