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?
3 comments
Log in to join the discussion
Log In3 Comments
jordangibson1mo ago
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_mason311mo ago
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
reeseanderson4d ago
Truth be told, list comprehension changed my whole workflow too. Used to write three lines just to filter and map something. Now I just type one line and it does the same thing faster. Feels like cheating but it's just better code.
5