This line process = matched_edges
is actually creating a reference to the matched_edges
list, not a copy of the items. In turn, this is affecting your iterator in the for loop as you remove items from process
(and matched_edges
). To demonstrate I slightly modified your code (just to print values):
matched_edges = [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
process = matched_edges
for tup in matched_edges:
print(f"Current tup: {tup}")
print(f"Process: {process}")
print(f"Matched: {matched_edges}")
if (tup[1], tup[0]) in process:
process.remove((tup[1], tup[0]))
if -1 in tup:
process.remove(tup)
Output:
Current tup: (0, -1)
Process: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (2, 3)
Process: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (4, 6)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (5, 7)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Current tup: (8, 9)
Process: [(1, -1), (2, 3), (4, 6), (5, 7), (8, 9), (9, 8)]
Matched: [(1, -1), (2, 3), (4, 6), (5, 7), (8, 9), (9, 8)]
You can see on the second iteration you end up skipping the (1, -1)
tuple. In general, you're skipping an item every time you remove one.
Change your assignment to process
to use copy()
so you get a new list:
process = matched_edges.copy()
This should fix the odd behavior. Although I'm not quite sure if the loop you're using still does exactly what you intend it to, but this is the output with the copied list:
Current tup: (0, -1)
Process: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (1, -1)
Process: [(1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (2, 3)
Process: [(2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (3, 2)
Process: [(2, 3), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (4, 6)
Process: [(4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (5, 7)
Process: [(4, 6), (5, 7), (7, 5), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (6, 4)
Process: [(4, 6), (5, 7), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (7, 5)
Process: [(5, 7), (8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (8, 9)
Process: [(8, 9), (9, 8)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]
Current tup: (9, 8)
Process: [(8, 9)]
Matched: [(0, -1), (1, -1), (2, 3), (3, 2), (4, 6), (5, 7), (6, 4), (7, 5), (8, 9), (9, 8)]