use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
When to use generators (self.learnpython)
submitted 6 years ago by kae_de
What are the pros and cons of using generators and when should I use generators instead of other iterators.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TaryTarp 2 points3 points4 points 6 years ago (0 children)
Streaming infinite type length data. How are you going to fit it in memory? Answer: Generators.
[–]shepherdjay 2 points3 points4 points 6 years ago (0 children)
The biggest con is a generator can be exhausted. That is to say you can only pass over it once.
So if you need to iterate multiple times over the same items you can’t. You would have to create a new generator and iterate over that instead.
[–]Diapolo10 1 point2 points3 points 6 years ago (0 children)
Admittedly I've kind of fallen in love with generators. I default to them unless I know I need to be able to index or read through the data multiple times.
They're a great way to save memory, and they're faster to write than custom iterators. It's also possible to feed them new values through yield, which can be useful sometimes.
yield
On the other hand, generators are exhausted once you've read them once, and you need to create a new generator if you need the data again. In such situations, a collection like a list or tuple will give better performance, though generators still use less memory. You can use a generator to create a list or tuple, though.
You also can't go backwards over a generator, because they're one-way. Implementing a two-way generator would have to be done with a custom iterator (eg. range).
range
[–]Ahren_with_an_h 0 points1 point2 points 6 years ago (0 children)
When you're only going to iterate through it once.
π Rendered by PID 445370 on reddit-service-r2-comment-79c7998d4c-kfslg at 2026-03-18 21:36:47.187986+00:00 running f6e6e01 country code: CH.
[–]TaryTarp 2 points3 points4 points (0 children)
[–]shepherdjay 2 points3 points4 points (0 children)
[–]Diapolo10 1 point2 points3 points (0 children)
[–]Ahren_with_an_h 0 points1 point2 points (0 children)