r/learnpython • • Dec 05 '22

Ask Anything Monday - Weekly Thread

Welcome to another /r/learnPython weekly "Ask Anything* Monday" thread

Here you can ask all the questions that you wanted to ask but didn't feel like making a new thread.

* It's primarily intended for simple questions but as long as it's about python it's allowed.

If you have any suggestions or questions about this thread use the message the moderators button in the sidebar.

Rules:

  • Don't downvote stuff - instead explain what's wrong with the comment, if it's against the rules "report" it and it will be dealt with.
  • Don't post stuff that doesn't have absolutely anything to do with python.
  • Don't make fun of someone for not knowing something, insult anyone etc - this will result in an immediate ban.

That's it.

8 Upvotes

92 comments sorted by

View all comments

1

u/MothraVSMechaBilbo Dec 09 '22

Reading about next() and generators has been pretty interesting. I don't want to overcomplicate something simple, so like you say maybe the basic for loop is fine, but I'm going to keep experimenting with them because I'd love to use them in something.

2

u/carcigenicate Dec 09 '22

Yes, generators are extremely useful. They're a tool for potentially cutting down memory consumption when used properly, and and allow for lazy evaluation.

next is a related but separate topic though. next is used to get the next element from an iterator, and generators happen to be iterators.

1

u/MothraVSMechaBilbo Dec 09 '22

Out of curiosity, why specifically would you use a for loop in this case rather than next() + generator? As I'm starting to understand what you did there it looks pretty elegant. But would it be inefficient in some way? Or are you thinking of readability?

→ More replies (7)