T
14

Back when I started, I thought a for loop was the only way to print numbers

I was messing around in Python last night, trying to make a list of squares. I wrote a for loop, like I always have for years. Then I saw a note in an old book about list comprehension. I tried it, just to see. Typed '[x*x for x in range(10)]' and ran it. It worked instantly, same result as my five lines of loop code. Felt weird, like I'd been doing extra work for no reason. It made me think about how I learned coding from these big, slow books in 2010. Now you can find a better way in two seconds online. Has anyone else had a moment where a simple trick from a new tutorial totally changed how you do a basic task?
2 comments

Log in to join the discussion

Log In
2 Comments
jordangibson
Honestly, I used to write a whole function to check if a list was empty. Then someone showed me just typing "if not my_list". Felt like a magic trick.
6
gavin_mason31
Oh man @jordangibson, that hits home. I had the same exact moment but with checking for None. Wrote all these clunky "if variable is not None" lines forever. Then you see that empty list or zero just reads as False and your whole brain rewires. It's those little bits of clean code that just feel good.
4