T
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?
2 comments

Log in to join the discussion

Log In
2 Comments
ryan_ellis
ryan_ellis12d 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_butler27
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