18
Tried using Python's for loop to reverse a list and got garbage output
I spent an hour trying to reverse a list with a basic for loop instead of using [::-1] and ended up with a bunch of None values because I was appending in the wrong spot. Has anyone else overthought a simple slice operation and made it way harder than it needed to be?
3 comments
Log in to join the discussion
Log In3 Comments
ryan_ellis1mo ago
Man that's the classic trap. I did the same thing trying to manually reverse with a while loop and index counters. Ended up with an infinite loop and had to ctrl-c out of it. The slice trick is just so clean once you realize it exists.
5
hayden_butler271mo ago
Three years of manual loops and index math. Then I found slice() and felt like an idiot for not learning it sooner. Changed my whole approach.
8
lisa524d ago
Oh man I once spent an entire afternoon debugging a reverse loop only to realize I was accidentally setting my index back to zero every iteration. Felt like a genius when I finally just used slice.
3