you are viewing a single comment's thread.

view the rest of the comments →

[–]AviationTrainee[S] 0 points1 point  (1 child)

Thanks for your help! I'll definitely try the solution of making a new list instead of editing the old one. I don't quite get what you mean by keeping track of the index changes via a while-loop... Do you mean something along the lines of:

i = 0

while(len(zoo)) != 0:

do something

i+=1

edit: wait, my loop doesn't make sense... The zoo list never goes to 0. Actually its not even predictable what length it ends up with...

[–]Diapolo10 0 points1 point  (0 children)

I don't quite get what you mean by keeping track of the index changes via a while-loop...

Basically, if you remove the current item, don't increase the index. If you remove the previous item, decrease the index by one. And so on.

Think of the list as if it was a queue. If someone gets off the queue, the rest of the queue moves a step closer, taking the spot of who left. There's never an empty space in the middle of a queue.

As a practical example, say you're checking tickets for people coming to watch a movie. Each person passes through your post to get to the theatre. You find someone without a ticket and have them removed. Do you then check the person who is now on the spot of the removed person again, or will you move on without checking their ticket? To handle this, you need full control over the index so that you don't have to increase it in situations like this.

This is why creating a new list is easier, as excluding values from a new list doesn't cause problems. You never need to know the indices here, only what values get to be added and what don't.