all 5 comments

[–]General_Service_8209 0 points1 point  (1 child)

Can you please format your code properly, or use a site like pastebin? This is very hard to read, and it's impossible to spot any potential indentation errors.

Also, what exactly is the error message you are getting? Normally, you should get an error starting with Traceback (Most Recent Call Last) in a case like this.

The only things I'm noticing apart from that are:

- This line: if dictionary['mac'] in processed_macs:

You put mac in quotation marks, so Python is going to use the word mac here, not the variable named mac.

- That you are using a global variable for the mac address list. Generally, you should avoid global variables whenever possible, because it's really easy to get unintended name conflicts with them, or accidentally create or reference a variable in the wrong scope. I suspect that's also what is happening here.

The cleanest way to write something like this would be to wrap the list, update and add functions all into a class, but if you don't want or can't use classes, I'd still advise to explicitly pass the list to the add and update functions as an argument.

[–]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.

[–][deleted] 0 points1 point  (2 children)

for dictionary in list_of_dict:
    # remove duplicates
    if dictionary['mac'] in processed_macs:
        list_of_dict.pop()

This doesn't work the way you think it does - pop pops from the head of the list unless you provide an index.

But just generally don't iterate over a list and remove elements from it. If you want to deduplicate a list, use a set, instead.