I feel like this should be a pretty simple process but for some reason I cannot get it to work.
Goal: Loop through a list of objects and if they match a condition remove it.
Issue: This works until it needs to remove all objects from the list. It will not remove the last item.
Example (clearly simplified by same results):
def some_function(val): return True on_shift2 = ['user1', 'user2', 'user3'] print("MY ON SHIFT2", on_shift2) for idx, val in enumerate(on_shift2): print(f"Index: {idx} & Value: {val}") if len(on_shift2) == 1: del (on_shift2) # remove the whole list else: check_user = some_function(val) # function to check if user is in list and if so return true if check_user: on_shift2.remove(val) # Also tried the following # del on_shift[idx] # Same result # on_shift.pop(idx) # Same result print("MY ON SHIFT2", on_shift2) Results:
MY ON SHIFT2 ['user1', 'user2', 'user3'] Index: 0 & Value: user1 Index: 1 & Value: user3 MY ON SHIFT2 ['user2'] Now the really odd part here is no matter what is in the list, it always seems to keem the middle item (i.e. user2). If i changed those values to chicken, horse, dog - the remaining item would be horse. Not sure if that's important but thought i would bring it up.
Also, no matter the which method i choose to remove the the items from the list (pop, remove or del), the 3rd (or last) item never gets removed if they all return true from "some_function()".
Also when looping through the if condition on the len(on_shift) never does equal 1, it always stops at 2. Never makes it to the 3rd (or last) object if the all return true from "some_fuction()", so my if statement to delete the list never happens.
If i print the val for the loop without removing anything from the list, all 3 names show up. I have to be doing something silly here.
Thoughts?
for idx, val in enumerate(on_shift2.copy())copy and then delete, your if conditionif len(on_shift2) == 1is not needed anymoreNameError: name 'on_shift2' is not definedif check_user:line and see what happens. I guess the problem is in this line it returns false sometimes and all items do not get deleted.if len(on_shift2)clause.