19
I used to write my own loops for everything, but then I tried list comprehensions in Python.
I mean, my old way took like 5 lines to filter a list, but a list comprehension does it in one clean line. It just makes the code so much easier to read. Has anyone else found a small syntax change that made a big difference for them?
3 comments
Log in to join the discussion
Log In3 Comments
mia_thompson131mo ago
Oh man, totally. It's like a lightbulb moment. I was the same with string formatting, doing the old .format() thing forever. Then I saw @troy_jenkins point about f-strings and tried it. Stuff like f"Hello {name}" instead of "Hello {}".format(name) just feels so much clearer. You see the variable right there in the string. It seems small but it makes the whole code block less cluttered to read through.
10
the_derek24d ago
Yeah, the f-string thing is huge. I read a blog post that called the old .format() way "string archaeology" because you have to dig to match up the placeholders. Seeing f"Total: {price quantity}" with the math right inside the brackets just kills me. It's one of those things that makes you wonder how you ever put up with the old way.
6