1

I am new to python, I want to know the concept of generator and iterator

Please somebody explain what is this and how it works with some nice examples.

Aman Tandon
  • 1,143
  • 1
  • 11
  • 24

1 Answers1

2

I will direct you to two great answers on SO:

Understanding Generators in Python

and

Difference between Python's Generators and Iterators

Finally, this article is what finally got me to understand it properly:

What does the "yield" keyword do in Python?

Community
  • 1
  • 1
idjaw
  • 21,992
  • 6
  • 53
  • 69
  • Thank you so much. Just to clear... every function which uses an yield method will be an generator right? And in generator code when we will call next() code will execute till yield and return value from the yield, and so on.. – Aman Tandon Sep 26 '15 at 12:53
  • You're on the right track. But, this is exactly what I read when I learned generators, iterators and yield. This is the article that helped me the most. I will also put it in my answer. http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python – idjaw Sep 26 '15 at 13:00
  • 1
    thanks this post was nice. – Aman Tandon Sep 26 '15 at 13:14
  • To visualize everything, this is good tool. http://pythontutor.com/visualize.html#mode=display – Aman Tandon Sep 27 '15 at 03:47