To remove elements from a list while iterating over it, you need to go backwards:
if delLater == True:
for x in schedule[-1::-1]):
if 'DELETE' in x:
schedule.remove(x)
A better option is to use a list-comprehension:
schedule[:] = [item for item in schedule if item != 'DELETE']
Now, you could just do schedule =
instead of schedule[:] =
-- what's the difference? One example is this:
schedule = [some list stuff here] # first time creating
modify_schedule(schedule, new_players='...') # does validation, etc.
def modify_schedule(sched, new_players):
# validate, validate, hallucinate, illustrate...
sched = [changes here]
At this point, all the changes that modify_schedule
made have been lost. Why? Because instead of modifying the list object in place, it re-bound the name sched
to a new list, leaving the original list still bound to the name schedule
in the caller.
So whether or not you use list_object[:] =
depends on if you want any other names bound to your list to see the changes.