I have the two lists below:
list1 = [(1, 'A'), (2, 'B'), (3, '?'), (4, 'T'), (5, 'A'), (6, 'B'), (7, '?'), (8, 'T')]
list2 = [(2, 'D'), (6, 'E'), (7, '!'), (10, 'T')]
I need to remove from list1
the elements in common with list2
that are in first position. So, I've used the for loop below:
for x in list1:
id_list1 = x[0]
for y in list2:
id_list2 = y[0]
if id_list1 == id_list2:
list1.remove(x)
The output is: [(1, 'A'), (3, '?'), (4, 'T'), (5, 'A'), (7, '?'), (8, 'T')]
The result is strange because I see (7, '?')
in list1
but not (2, 'B')
and (6, 'B')
. The last two tuples are correctly removed, but why not (7, '?')
?
The expected output is: [(1, 'A'), (3, '?'), (4, 'T'), (5, 'A'), (8, 'T')]