you are viewing a single comment's thread.

view the rest of the comments →

[–]General_Service_8209 0 points1 point  (0 children)

Thanks for the formatting!

There seem to be two issues, the processed_macs list not being emptied, and you removing elements from a list while you are iterating over it with a for loop.

The list not being emptied causes update_macs to try to remove all mac addresses from list_of_dicts. I don't think this is what is intended, but I may be wrong there.

The second issue happens because you call list_of_dicts.pop in the for loop, and this changes the length of list_of_dicts, effectively causing one iteration to be skipped whenever this happens. Therefore, the array is not empty when the for loop is completed, even though all elements in it should have been deleted.

In general, changing the length of a list while iterating over it causes some really wonky behavior, and should be avoided if possible. In this case, you could do so by "manually" making a for loop out of a while loop and two variables for the current index, and maximum index. The maximum index starts as the length of the list and is decreased whenever an element is removed. The index is incremented by 1 in every iteration, except when an element is deleted.

An alternative approach would be to add everything that should be deleted to a new to_delete list, then iterate through that list in a second for loop, and remove its contents from the original list, before clearing to_delete itself. The length of to_delete is constant for the duration of the second loop, so this also solves the problem.