17
I was writing loops all wrong for 6 months
Started learning Python back in January and I kept getting these weird bugs where my for loops would skip the last item in a list. I figured it was just a quirk of the language. Then last week I was helping a buddy debug his code and he pointed out I was modifying the list while iterating over it. I had no idea that was a no-go. Turns out Python keeps track of the index position, so when you remove an item, everything shifts and stuff gets skipped. Felt like an idiot for not catching it sooner. Anyone else have a basic concept suddenly click after months of doing it wrong?
1 comments
Log in to join the discussion
Log In1 Comment
veraramirez19h ago
Kicked myself so hard when I realized I was doing the exact same thing with dictionary keys. Had this loop that was supposed to clean up old entries and kept wondering why half of them were still hanging around. Felt like I'd been writing code with a blindfold on for months. Funny how one little "oh wait" moment makes you feel both smart and dumb at the same time.
5