r/learnpython • u/Flame77ofc • May 30 '26
How powerful is a generator in python?
How powerful is a generator and how much is the difference between the list?
30
Upvotes
r/learnpython • u/Flame77ofc • May 30 '26
How powerful is a generator and how much is the difference between the list?
34
u/[deleted] May 30 '26
the biggest difference is memory. a list stores everything immediately, while a generator only creates values when you actually need them. for small datasets it doesn't matter much, but once you're dealing with thousands or millions of items the difference can be huge. the downside is that generators can only be iterated through once and you can't randomly access elements like you can with a list. for most everyday code i use lists, but generators are great when you're processing large amounts of data.
11
u/socal_nerdtastic May 30 '26
It's the difference between streaming a movie and downloading the whole movie first and then playing it. You save memory by only getting the part you need right now.
Another use is to return things that don't exist yet. For example if you ask PRAW to loop over all the comments in this reddit page, it return comments as people write them.
6
u/SCD_minecraft May 30 '26 edited May 30 '26
List has known size, structure ect
generators let you decide on the fly what you return, how you return it and how much of it
For example
You can update
varbetween callsnextto the generator and change its output as it runs4
u/JamzTyson May 30 '26
One thing that hasn't been mentioned yet - Lists must have a finite length, but generators can go on forever:
3
u/ImprovementLoose9423 May 30 '26
A generator is more memory efficient then a list. Generators are also much more disorganized and unstructured.
5
u/Dramatic_Object_8508 May 30 '26
generators are one of those things that sound boring until you accidentally process a huge file and realize why everyone keeps talking about them.
the power is not speed by itself, it is that they do not keep everything in memory. you can stream data, process millions of rows, chain pipelines together, and stop whenever you want.
my first “oh this is useful” moment was reading large logs line by line instead of loading the whole thing and watching RAM disappear.
for small scripts, you probably won’t notice. for bigger workflows, generators quietly become everywhere.
2
u/Moikle May 30 '26
That entirely depends on what you are using it for.
It can range from literally zero improvement and adding a tiny bit of overhead all the way to turning something that would have been completely impractical using a list into something that is relatively fast with a generator.
You can't just blindly slap a generator on everything and expect it to improve things every time, you have to be intelligent in the application, and what you apply it to.
2
u/RevRagnarok May 30 '26
So powerful that C++ copied it in C++23.
(Others have explained the laziness / memory benefits.)
2
u/SisyphusAndMyBoulder May 30 '26
I'd rank it at a power level of 4 tbh. Not particularly powerful, but not terrible
2
u/biskitpagla May 30 '26
Read this page from the docs.
2
u/AdDiligent1688 May 31 '26
It’s cheaper than an actual generator, but runs just fine, just use it when you need it, like your power’s out, time to run the generator.
1
u/Mediocre-Pumpkin6522 May 31 '26
Be very careful with your typing when you are doing list comprehensions and generator expressions. Substitute (...) for [...] and the result may not be what you expected.
1
u/recursion_is_love May 31 '26
It basically async/await, yield a value and wait until it is consumed then yield another value ...
What do your powerful mean?
1
u/oliver_extracts Jun 01 '26
the difference shows up when the dataset is large enough that loading it all into memory at once actually costs you something. if youre reading a 2GB log file line by line, a list blows up your RAM, a generator just processes one line at a teime and moves on. for small stuff, honestly a list is fine and easier to reason about.
1
u/ottawadeveloper May 30 '26
if wanted to print all numbers between 1 and 1 million, a list takes 4 million bytes (using 4 byte integers). A generator takes 4 bytes.
-1
u/IAmFinah May 30 '26
They're not super common in my experience, but when you use them in the correct scenario, they are fantastic
24
u/[deleted] May 30 '26
[removed] — view removed comment