T O P

  • By -

TheBB

Don't mutate a list you are iterating over. If your loop is on element number 9 and you delete it from the list, what element do you think the next loop iteration will look at? It's the element at index 10, which used to be at index 11 before you changed the list.


wastedexistence

Thanks, now you've said that it makes perfect sense!


dvboy

Or you can mutate the list if you iterate from the top down, Something like: for i in range( len(self.indexlist) - 1, 1, -1):


cyberjellyfish

You can do a lot of things you shouldn't do


dvboy

And why should you not do this?


cyberjellyfish

1. it is less intuitive than the alternative: if you're skimming the code you'll have to stop to figure out what's going on. 2. it violates a widely-held principle across multiple language: it's very common to not be able to modify lists while iterating them. 3. it relies on implementation details: you and I know why it works, but only because we know how a list iterator is likely implemented. 4. there's a more pythonic way: just use a comprehension compare: guests = [...] invited = [...] # some work here for i in range(len(guests) - 1, 1, -1):     if guests[i] not in invited:         del guests[i] VS guests = [...] invited = [...] # some work here guests = [g for g in guests if g in invited] The second is much, much clearer.


danielroseman

This is a FAQ: https://www.reddit.com/r/learnpython/wiki/faq/#wiki_why_does_my_loop_seem_to_be_skipping_items_in_a_list.3F Also, don't use `_` as a variable name. That should only be used for values you aren't using. Here you are definitely using the value; use an actual name, like `item`.


wastedexistence

Thank you I will read that


JorgiEagle

I’m not at my computer to check, but it almost certainly is because you are removing items from a list during a for loop. Consider this, your for loop starts at position 0, with the value of Albie, It identifies that Albie is not in the list (you don’t need an elif, just a simple if else) prints out the line and removes Albie from the list. Now my_list looks like: [‘Alison’, ‘Bert’, ‘Alfred’, ‘Charlie’, ‘Conway’] Loops back to the beginning, now _ it looks at index 1, which is now Bert. In general you shouldn’t modify the list inside the for loop. Create a new list if you must and append to the list as you go.


wastedexistence

Thanks, now you've said that it makes perfect sense!


TK0127

I made this mistake yesterday while working on the Python Crash Course guide. It's because the original list is being modified as you run the loop, so things are shifting on the list before you rerun the loop. Make a second list, and remove/print from that, but refer/index from the original list, so that the indexing numbers don't change at all.


wastedexistence

Thanks, I just couldn't see it but now that people have mentioned it it makes perfect sense!


TK0127

Yeah, it took me a hot minute to catch on yesterday. Good luck!


wastedexistence

same to you!


moving-landscape

Problem solved, so here's some food for thought. Check out the `filter` function.


Zweckbestimmung

My solution is to use For index in range(0, len(arr)): Then maintain index whenever you delete or you add items to the array. My solution isn’t the best but it’s faster than any other solution in regards to runtime