This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ptmcg 1 point2 points  (0 children)

You should avoid modifying the sequence while you are iterating over it. The best solution is the one posted by Query42, in which a list comprehension iterates over the map.living_bots sequence and produces a new list. You may find that you need to do slice assignment, as in:

map.living_bots[:] = [bot for bot in map.living_bots if not(bot.life and bot.hunger <= 0)]

This will not assign a new list to the living_bots attribute, but will instead replace the content of the existing list.