Python › Iterators & the Iterator Protocol
What is the difference between an iterable and an iterator in Python?
BeginnerAnswer
An iterable is any object implementing __iter__() that returns an iterator, such as lists, tuples, and dicts -- you can loop over it repeatedly. An iterator is an object implementing both __iter__() (returning itself) and __next__() (returning the next value or raising StopIteration) -- it's stateful and exhausted after one full pass.
nums = [1, 2, 3] # iterable
it = iter(nums) # iterator
print(next(it)) # 1
print(next(it)) # 2Real-world example A file object is an iterator over its lines -- you can only read through it once before needing to reopen or seek back to the start.