MediumExperience level: 3-5 years · 8-15 years1 min read

What is the difference between an iterable and an iterator?

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitte
#iterator#iterable#iter#next#protocol
Report issue

Short Answer

An iterable is anything you can loop over — it implements `__iter__` (list, str, dict, set). An iterator is the object that actually produces items one at a time via `__next__` and raises StopIteration when done. `iter(iterable)` gives you an iterator; a `for` loop does this for you.

On this page

Concept Made Simple

Coffee Chat Question

“What is the difference between an iterable and an iterator?”

Remember It Faster

Mind Map Answer

Iterable
has __iter__ (list, str, dict)
Iterator
has __next__, tracks position
iter(x)
iterable → iterator
StopIteration
signals 'no more items'

Learn by Doing

Hands-on Keyboard

python
nums = [10, 20, 30]      # iterable
it = iter(nums)          # iterator
print(next(it))          # 10
print(next(it))          # 20
# for x in nums: ...     does exactly this internally
Output
10
20

Think Beyond the Expected

What If?

What actually happens under the hood when you write `for x in my_list`?

Python calls iter(my_list) to get an iterator, then repeatedly calls next() on it, binding each result to x, until StopIteration is raised — which the for loop catches to end. So every for loop is sugar over the iterator protocol (__iter__ + __next__).

Real World

Understanding the protocol explains why generators, file objects, and range work in for loops, why an iterator is single-pass, and how to build custom iterable classes for your own data structures.

Interviewer's Expectation

Keywords they're listening for:

  • iterable has __iter__
  • iterator has __next__
  • iter()/next()
  • StopIteration ends loop
  • for loop uses the protocol

Common Mistakes

  • Confusing iterable with iterator
  • Expecting an iterator to restart
  • Forgetting StopIteration in a manual __next__

Best Practices

  • Implement __iter__ to make classes loopable
  • Prefer generators for custom iteration
  • Remember iterators are single-pass

Follow-up Questions

  1. Is a generator an iterator?
  2. Why can you loop a list many times but a generator only once?
  3. How do you make a custom class iterable?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Iterators (Python)
Interview question: "What is the difference between an iterable and an iterator?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.