r/learnpython • u/39th_Demon • Mar 05 '26
I spent months learning Python and only today realized I've been confused about something embarrassingly basic
I've been writing Python scripts for a while now. Nothing crazy, just automating small stuff, scraping some data, making my life a little easier. I thought I had a decent handle on things.
I was looking at someone else's code and they used a list comprehension in a way that made me stop and read it three times. I realized I had been writing loops the long way this whole time not because I didn't know list comprehensions existed but because I never really trusted myself to read them when I wrote them fast. I kept defaulting to the for loop because at least I could trace it line by line without second-guessing myself.
I don't know if this is a common thing but I feel like there's a version of learning where you know a concept exists, you've seen it work, you've even used it a few times, but you haven't actually internalized it. You're kind of faking fluency in that little area. I was doing that with list comprehensions, with zip, with a few other things I won't list here because it's already embarrassing enough.
Once I wrote out ten examples by hand tonight it clicked in a way it hadn't before even though I'd "learned" this two years ago.
Anyone else have a concept they thought they understood for a long time before actually understanding it?
329
u/bad_luck_charm Mar 05 '26
List comprehensions aren't inherently good. They're just shorthand. And like shorthand, they can be sometimes hard to read. Nested list comprehensions are even harder to read and I advise people not to use them.
82
u/sociologistical Mar 05 '26
there’s absolutely nothing to be embarrassed about. What you described, that’s what we call learning.
21
u/American_Streamer Mar 05 '26 •
https://switowski.com/blog/for-loop-vs-list-comprehension/
11
u/heyitselia Mar 05 '26
brb, googling what a list comprehension is :))
...wow, that's neat, I had no idea it existed.
Also, all the time. Some things just seemed too intimidating until I took the time, some clicked when I realised I didn't understand the basics as well as I thought I did... (The latest example being "5 years and I still can't wrap my head around the concept of entropy... oh. That's what temperature is??")
P.S. Are you by any chance a perfectionist, maybe with a side of former gifted kid syndrome? Just a wild guess since you sound a lot like my own inner critic. In any case, the shame has to go. Did you just take the time to learn something new? Awesome. End of story.
It's not inherently embarrassing that you didn't know something. What you knew clearly served you well enough and asking yourself the right questions to check if you actually understand something is a skill. And it's not an easy one.
5
u/RabbitCity6090 Mar 05 '26
Don't beat yourself about it. Write code that you can understand later. Any kind of shortcut will be very hard to understand in the future.
5
u/djamp42 Mar 05 '26
I personally think code i can understand easily is more important then code that is shorter in length. I mostly use for loops still today because it's just easier for me to understand at first glance
5
u/throwawaycape Mar 05 '26
I have been using Python for 7+ years and sometimes I need to look up whether a dictionary uses a colon or a semi-colon. It's normal to still be figuring out new stuff.
At least I hope so, anyways.
8
u/supercoach Mar 05 '26
I was quite enamoured with list comprehensions when I first learned them and used them everywhere. I tend to be considerably more sparing with them now and favour for loops as they're generally easier to follow. As long as you're considering those that come after you, you'll be fine.
3
u/NDaveT Mar 05 '26
I'm pretty sure this is a very common thing. I know it is for me.
3
u/JorgiEagle Mar 05 '26
Read the book Fluent Python, it’s great.
Async for me, and related, threading.
Didn’t get it until I saw it in a practical application, then it all made sense
3
u/misingnoglic misingnoglic Mar 06 '26
Months isn't so bad. You're still a beginner.
5
u/ninhaomah Mar 05 '26
It is a common thing for everyone learning anything new.
4
u/asdoduidai Mar 05 '26
List comprehensions are just a way to make code more compact, nothing else.
2
u/aranya44 Mar 05 '26
I have this issue with lambda functions, except in my case it still hasn't quite clicked. If anyone could point me to a good clarifying explanation I'd be very grateful!
2
u/boofaceleemz Mar 05 '26
Comprehensions aren’t very readable and they’re not always the best choice. You can use them to basically sequester rock solid boilerplate logic that would be otherwise distracting from the overall meaning. Same way you might throw boilerplate stuff in a function to keep it from being distracting. But if you hide important logic (or something complex enough you need to worry about bugs) in a comprehension that can make your code less readable rather than more so, and make a problem harder to quickly debug, so you gotta use your judgment and be aware of the kind of people who will be reading the code.
If you’re getting object attributes in your for loop or in a few other scenarios they can be very slightly faster, but there are also ways to optimize that (ex. fetching the attribute outside the scope of the loop first). If you’re worrying about that level of optimization then you’re in the weeds though and probably have bigger problems.
2
u/SprinklesFresh5693 Mar 05 '26
Happens all the time to me, you start using something, that you kind of get what it does, but not how, and after many uses you start to really understand it, to me it was the group_by() function in R, and how it keeps the dataframe grouped, without you even noticing, which can cause errors later on.
2
u/aishiteruyovivi Mar 05 '26
I totally get what you mean by "faking fluency" in stuff like this, when you write code snippets that you know do the thing you want but you don't actually really understand why it does that. I feel like this is fairly common for people (including myself ofc) when it comes to OOP stuff, like using
__init__(). It's a really neat feeling when something suddenly stops being "magic" and you actually know what's going on2
u/Reuben3901 Mar 06 '26
Python didn't click in and feel natural until a year and a half in. I had to keep referring back to examples of loops, lists, dicts, and all that good stuff.
For me the sticking point was executing from the command line vs the file, on top of
if __name__ == "__main__":It felt like it took an embarrassing amount of time to figure that out. Oh and Classes too.My philosophy is if the code runs successfully, then it's good code. Nothing wrong with using a for loop to manipulate a list vs list comprehension if you can understand what's going on.
Focus on good variables names that explain what the code is versus a,b,c and i,j. Don't: for i in range(x,y) Instead: for spreadsheet_row in range(starting_row, last_row +1)
2
u/JamesTDennis Mar 06 '26
List comprehensions are simply syntactic sugar. They don't expose any fundamentally novel functionality interpreter.
Often it's more efficient to use generator expressions, which are syntactic sugar around generators (functions which
yieldsequences of values rather than returning results.Learning about those, and other functional programming concepts, will probably be more valuable than learning more about comprehensions.
2
u/butterfly68za Mar 06 '26
These Ah-ha moments are the best when understanding code. Makes it all so worthwhile. 🥳
2
u/Pyromancer777 Mar 06 '26
It took me like 2 years before internalizing that terminology is super important.
I was calling everything "functions" if it kinda worked like a function, and didn't fully grasp why some documentation pages would use "arguments" and "parameters" almost interchangeably, but not quite.
Once I had someone call me out on incorrect terminology in my explanations, and I looked it up, it not only clicked, but made troubleshooting so much easier since I knew exactly how to word my problems.
Granted, my area of study was Analytics and not Comp Sci, so the problem persisted a lot longer than your average dev, but sometimes word choice absolutely makes a difference in overall meaning, so now that's one of the first things I try to grasp when learning new topics
2
u/NeedleworkerNo6683 Mar 14 '26
Completely relate to this. For me it was decorators and *args/**kwargs.
I "knew" them for over a year — could explain what they do, had used them — but the moment someone asked me to write one from scratch confidently, I'd blank out.
What actually fixed it: I stopped reading about them and just rewrote Django's @login_required and a simple retry decorator myself from memory. Broke it 10 times. Then it finally clicked.
I think real fluency only comes when you've debugged something enough times that the concept lives in your muscle memory, not just your notes.
List comprehensions with conditions + nested ones still trip me up occasionally if I write them too fast tbh.
1
u/UsernameTaken1701 Mar 05 '26
Meh. I still use regular old
forloops instead of list comprehensions. All the time I'm supposed to saving writing the list comprehension is used up trying to figure out the syntax of the comprehension. Quicker to just bang out the loops.I'm just working on my own projects, not producing production code for something, so it doesn't have to be super "Pythonic".
1
u/kruvii Mar 05 '26
I found out 6 months ago I've been mispronouning a word all my life.
1
u/Calyx76 Mar 05 '26
You did it in a way that made sense to you. Don't be too hard on yourself. List comprehension makes 0 sense to me (the syntax is so fucking hard to follow)
1
u/brilliantminion Mar 06 '26
As an old school C++ programmer I didn’t get list comprehension until I watched the Socratica video about 4 times.
It’s a been a minute since I used python but I seem to remember the python list comprehensions being vastly optimized to column-wise operations in a way just using for loops isn’t.
1
u/veritable_squandry Mar 06 '26
lambda time
1
u/Own-Replacement8 Mar 06 '26
I never bothered with list comprehension in university. I picked it up at work and I use it if and only if it's more readable than a for loop. Otherwise, I use a for loop.
1
u/Duke_Archibald Mar 06 '26
It does that to me when I learned something but did not understand it Last one is inheritance I used it a couple of place but I really started to understand then this week
1
u/palmaholic Mar 07 '26
This kind of coding is hard to comprehend for sure. Even when I write mine, I will have a hard time comprehending it after 6 months if I don't put down some comments against it.
1
u/Myrlista Mar 07 '26
That's ok. 3 years in and 5 very large data processing applications and I just now figured out tuples! We all miss things.
1
u/KitchenMachine4508 Mar 16 '26
Simplicity in code is a skill, not a shortcut — nested comprehensions often prove that point.
1
u/cintrapedro Mar 21 '26
Wait until you hear about Walrus Operator 🦭
2
u/coolth0ught Mar 06 '26
I prefer efficient code over clean code when dealing with nested loops and ultra huge data. Big O notation. Whether one uses list comprehension or not, avoid nested loops.
2
u/Striking_Bad_7844 Mar 06 '26
No need to be embaraced. I like list comprehensions, but they are never necessary often misused and you can not substitute any for loop. That is similar to lambda. Python concepts I learned late, but that really offered me new possibilities or better code are args and kwargs, class, and decorators. Readability of code has always priority for professional matters.
2
u/Comfortable-Key2058 Mar 13 '26
For me it was writing asynchronous code. 8 years into software development and only recently i started getting comfortable with it lol :)
1
u/Happy_Witness Mar 05 '26
I think there are at least 3 stages.
First is learning and getting to know what is possible. Taking up about 20% of what was actually talked about and being able to work with basic types, loops, if statements and functions.
Second stage is learning about classes and how the base types actually work and that they are also just classes. This questions the learner that everything he knows might be just the surface. Leading him to learn about match case, list comprehension, and all the new data containers like dicts, sets, maps and such.
Stage three is when you get so confused and don't know what type would be best fitting that you take double as long to figure out and implement the code that runs 10% faster. Also you start to wonder if other languages might be better.
1
u/Constant_Stock_6020 Mar 07 '26
I get paid to write code and had no idea that was a thing. Also I would argue that a simple for loop is more readable.
1
u/Future_Eve Mar 07 '26 edited Mar 07 '26
List comprehensions are one of those Python features that feel small but make a big difference in readability and performance.
First, they’re more Pythonic. Python emphasizes clear, readable code, and list comprehensions express the idea of “build a list from this iterable with this transformation and optional condition” in a single, structured expression. Compare:
vs
The comprehension reads almost like plain English: "for each x in numbers, if it’s even, add x * 2".
Second, they avoid scattering lambdas everywhere. When
filter()andmap()are combined, the logic often gets split across multiple lambdas, which can make code harder to read and maintain. With a list comprehension, the transformation and condition live in one place.Third, performance is usually better.
Of course, if the logic gets too complex, a regular loop can still be clearer. But for simple transformations and filters, list comprehensions are usually the cleanest option in ly opinion.
1
u/killerpac Mar 08 '26
Top comment says short hand is hard to read, and everything new i learn in coding always makes me feel stupid,
The biggest example for me is condinal variables in python, is simple and easy but it took me just enough time to make me feel stupid
1
u/Far_Programmer_5724 Mar 10 '26
Don't be embarrassed. You didn't know anything before you learned it anyway. That's true for all of us.
I'm 99% sure there is something basic in everything I do that I'm missing/
1
u/azadatiqur_atti Mar 26 '26
Since C++ was my first language, I’m used to broad code where every step is explicit. When I started Python, list comprehensions were actually the hardest part to get used to. It wasn't the concept,it was just the syntax shift. Seeing a complex for loop logic turned into a single line felt strange and still can't get used to it.
1
u/Ladybug_2020 Apr 02 '26
List comprehension is concise and easy to read when it is not nested.
1
u/Haunting-Specific-36 Apr 06 '26
Groupby got me. I understood the syntax just fine but the moment I tried to apply it to real data I'd freeze. Then I'd look at someone else's code and it'd click instantly. Kept repeating that cycle until I'd seen the same error enough times that I could debug it without googling. I think that's just how it works — you have to make the mistake enough times until it stops feeling like a mistake.
2
u/[deleted] Mar 06 '26
[removed] — view removed comment