Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Andrew Diodati3,256 Points
Mutating Lists Over Which We Are Iterating
This question is sort-of above and beyond the topic at hand, but I would like to ask it anyway.
I have been told that it is generally bad practice to mutate a list when you are iterating over it. This is why we often see a "copy" list when we run some loops. Why, then, is it acceptable to use .remove() on state_list (given the code from the video)? Is it because of the embedded for loop - so we are technically not iterating over state_list but rather vowels instead at that exact moment?
Ricky Catron13,023 Points
I would describe it like telling a track runner to run 100 meters then as they run dig a hole at 50 meters. Suddenly boom something they expected was not there. A copy of the list can be used so the loop doesn't hit the hole but you can still remove things as needed.
What I usually do instead is add the items I would have kept to a new list.