What is the key difference between an iterator and an iterable in Python?

Python Professional Hard

Python Professional — Hard

What is the key difference between an iterator and an iterable in Python?

Key points

  • Iterables return iterators with __iter__; iterators return values with __next__
  • Iterators implement both __iter__ and __next__; iterables only implement __iter__
  • Iterators raise StopIteration when no more values; iterables do not have a next value

Ready to go further?

Related questions