How can you create a generator in Python?

  • Using a class with __iter__ and __next__ methods
  • Using a for loop
  • Using a while loop
  • Using the yield keyword in a function
You can create a generator in Python by defining a function with the yield keyword. When the function is called, it returns an iterator that generates values one at a time when you iterate over it. This is a fundamental feature for working with large datasets efficiently.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *