0

I am newly learning Python. In Python we have Iterators and Generators for creating iterators or iterable items. My understanding is we can use Iterators or Generators for creating iterators in any usecase. I could not differentiate any usecase where specially Iterators should be used or only Generators should be used. Why Python has two approaches(Iterators and Generators) for creating Iterators. Is there any reason? In which scenario we need to use Iterators and in which scenario we need to use Generators?

Sorry for asking generic question, since I am newbie I could not understand the concepts in detail.

Thanks

user369287
  • 692
  • 8
  • 28
  • 3
    Maybe this Question/Answer may help: https://stackoverflow.com/questions/2776829/difference-between-pythons-generators-and-iterators – Alex Sep 22 '22 at 09:07
  • 1
    They aren't two approaches, generators are a convenient language construct for writing iterators ... generators *are iterators*. – juanpa.arrivillaga Sep 22 '22 at 09:17
  • 1
    From my understanding, iterator is a superset of generators. But generators will be helpful if you are working with large set of data. It basically works like a stream so that you don't have to store all the data at once to start processing it. Generators have a `yield` stmt as well. – defiant Sep 22 '22 at 09:18
  • 2
    @defiant The iterator is not a superset of the list, but the iterable object is. – Mechanic Pig Sep 22 '22 at 09:20
  • @MechanicPig aah ok. thanks for clearing that up.. – defiant Sep 22 '22 at 09:22

0 Answers0