I teach Python but igenuinely don't know how to explain list comprehensions in a way that actually sticks how did it finally click for you?
Posted by More-Station-6365@reddit | learnprogramming | View on Reddit | 9 comments
Hey r/learnprogramming this might be an unusual post but I genuinely need help here. I teach introductory Python at a college in the US. I'm comfortable with the language myself but I've hit a wall with one specific topic list comprehensions. Every semester I teach them the same way. I show the syntax, I show a regular for loop next to it, I explain that it's just a shorter way to write the same thing. Students nod. They seem to get it in class.
Then the next assignment comes and almost nobody uses them correctly. Half the class reverts back to regular loops even when a list comprehension would be cleaner. The other half tries to use them but writes the syntax completely wrong.
I've tried different approaches:
Showing the for loop first then converting it step by step into a list comprehension. Drawing it out visually on the board. Giving them fill in the blank exercises. Nothing seems to make it actually stick. Here's what I'm genuinely asking this community — especially if you learned Python recently:
Was there a specific way someone explained list comprehensions that finally made them click for you? How do you personally read the syntax in your head when you look at one? Is there an analogy or a way of thinking about them that made it feel natural?
I feel like the people who struggled to learn this have better insight into what actually works than most teaching material does. Any honest answer helps.
desrtfx@reddit
Your narrative doesn't add up:
What is it now? Civil Engineering junior, CS student who has been using Python for about a year, Teacher for 3 (several) years?
Every single of your post reads AI generated/worked over and looks like plain karma farming.
Malforus@reddit
Something I saw is that bots are doing non real time RAG to answer questions.
Literally data mining reddit users.
oclafloptson@reddit
What I'm hearing is that posts like this allow real-world programmers to influence the development of programming related LLMs
JamzTyson@reddit
The way I think about list comprehensions:
First we are building a list:
What do we want in that list?
We want
valueor a transformed valuef(value)Where do those values come from?
They come from iterating through an iterable
Do we want all of the values or do we want them conditionally?
A. We want all the values:
B. We want them conditionally:
vegan_antitheist@reddit
In mathematics, we would write this:
⟨ x² ∣ x ∈ numbers, x is even ⟩(I used angled brackets here to indicate it's not a set, but this isn't standardised)
In Haskell it's very similar:
[x^2 | x <- numbers, even x]Python has this:
[x*x for x in numbers if x % 2 == 0]These are all the same. A for loop is imperative. A list comprehension is declarative. I prefer monads because they are more general and often more readable.
flatMapflattens the data in many languages.[x for s in numbers for x in s]does what?!Anyway, I would teach that this is used to get away from telling the cpu what to do step by step and instead describe what you want.
vegan_antitheist@reddit
In mathematics, we would write this:
⟨ x² ∣ x ∈ numbers, x is even ⟩(I used angled brackets here to indicate it's not a set, but this isn't standardised)
In Haskell it's very similar:
[x^2 | x <- numbers, even x]Python has this:
[x*x for x in numbers if x % 2 == 0]These are all the same. A for loop is imperative. A list comprehension is declarative. I prefer monads because they are more general and often more readable.
flatMapflattens the data in many languages.[x for s in numbers for x in s]does what?!Anyway, I would teach that this is used to get away from telling the cpu what to do step by step and instead describe what you want.
desrtfx@reddit
Your narrative doesn't add up:
What is it now? Civil Engineering junior, CS student who has been using Python for about a year, Teacher for 3 (several) years?
Every single of your post reads AI generated/worked over and looks like plain karma farming.
chervilious@reddit
i think main issue is imperative vs declarative programming.
many people are having trouble with the idea of declarative programming. Which is basically what list comprehension are.
thebigmooch@reddit
For what it’s worth I had no idea about this and just googled it, very handy. You’ve taught me about their existence so good job!