ProgrammingHardGATE CS · Product-company placement

Which statement about iterables and iterators is correct?

  1. A. Every iterable is an iterator, because both define __iter__
  2. B. Every iterator is an iterable (its __iter__ returns itself), but a list is an iterable that is not an iterator
  3. C. A list is an iterator, because next() can be called on it directly
  4. D. A generator is an iterable but not an iterator, because it has no __next__

Correct answer: B. Every iterator is an iterable (its __iter__ returns itself), but a list is an iterable that is not an iterator

An iterator implements __next__ and an __iter__ that returns itself, so it is also iterable. A list only implements __iter__ (returning a fresh list_iterator); next([1]) raises TypeError. Generators are iterators.

More Python Programming questions

Exams that ask this topic