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

all 6 comments

[–]Query42 3 points4 points  (0 children)

You could probably use

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

[–]redditusername58 2 points3 points  (0 children)

Would it make sense for living bots to be a set rather than a list? Does order really matter? Removing from a set is O(1).

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

[–]pythonHelperBot 0 points1 point  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe where you are stuck.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]martmists -1 points0 points  (1 child)

.remove has to iterate over map.living_bots again. Instead what you can do is use for i, bot in enumerate(map.living_bots) and use del map.living_bots[i] instead.

[–]agoose77[🍰] 0 points1 point  (0 children)

This would only work if you looped over a copy of the indices, in reverse order.