Reputation: 93
I am confused why this short snippet to remove specific pairs from a list fails:
g = [[7, 11], [7, 8], [3, 10], [3, 8], [8, 9], [11, 10], [11, 2],
[11, 9]]
u = 3
g_cpy = g
for e in g_cpy:
if u == e[0]:
g.remove(e)
print g
It does not remove the entry [3,8], but it does remove [3,10]. What am I missing?
Upvotes: 0
Views: 1088
Reputation: 73480
g_cpy = g
does not create a copy of the list, but just a reference to the same object which you then modify during iteration. Another issue is that repeated remove
calls are bad in terms of time complexity (each call is O(N)
). It is better to use a comprehension to build the new list from scratch (overall linear complexity). You can still use that technique and mutate the original list by using slice assignment:
g[:] = [e for e in g if u != e[0]]
Upvotes: 1
Reputation: 596
I think the reason why [3, 8]
is not deleted is that
You deleted 3th element in array and e will be the 4th element in g_cpy
.
You already deleted [3, 10]
in g_cpy
so g_cpy
's 4th element will be [8, 9]
not [3, 8]
and this is why [3, 8]
isn't deleted.
If you copy list like g_cpy = g
it will just copy memory address. So both of them will point same object. If you delete item in g
item in g_cpy
will also deleted.
If you want to avoid this problem, copy list like g_cpy = g[::]
. It will copy entry object to other memory not just copying memory address.
Upvotes: 0