Medium👤 3-5 years👤 8-15 years 1 min read

What is the difference between an iterable and an iterator?

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.

Coffee Chat Question

Concept Made Simple

What is the difference between an iterable and an iterator?

🧠Mind Map Answer

Remember It Faster

Iterablehas __iter__ (list, str, dict)
Iteratorhas __next__, tracks position
iter(x)iterable → iterator
StopIterationsignals 'no more items'

⌨️Hands-on Keyboard

Learn by Doing

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

🔥What If?

Think Beyond the Expected

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 loopfor 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

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

🧩Related Technologies

generatorsitertoolsrange/enumerate/zip

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions