C
Python/Intermediate/Lesson 16

Iterator vs Iterable

15 min·theory
This chapter
8/8

Iterator vs Iterable

🎯 After reading this lesson

By the end of this lesson, you will be able to confidently do the following 3 things.

  • ✅ Build an iterable by implementing __iter__ · __next__
  • ✅ Understand which methods a for loop calls internally
  • ✅ Apply basic patterns from itertools

Keep the learning objectives as a checklist, and close the lesson once you can answer all of them.

Iterable vs Iterator — Code + Execution Results

The real reason a for loop works — __iter__ and __next__.


1. Iterable = "an object you can loop over"

python
# Iterable: list, tuple, dict, set, str, range, file, generator
for x in [1, 2, 3]:        # list
    print(x)

for c in "abc":            # str
    print(c)

# Commonality: has __iter__()
lst = [1, 2, 3]
print(hasattr(lst, "__iter__"))    # True

2. Iterator = "an object you can call next() on"

python
# iter() converts iterable → iterator
lst = [1, 2, 3]
it = iter(lst)             # Create iterator

print(next(it))            # 1
print(next(it))            # 2
print(next(it))            # 3
print(next(it))            # StopIteration exception

3. What a for loop actually does

python
# for x in lst: print(x) actually works like this:
it = iter(lst)
while True:
    try:
        x = next(it)
        print(x)
    except StopIteration:
        break

4. Building your own

python
class Count:
    def __init__(self, end):
        self.end = end
        self.n = 0

    def __iter__(self):
        return self                # Itself is the iterator

    def __next__(self):
        if self.n >= self.end:
            raise StopIteration
        self.n += 1
        return self.n

for x in Count(3):
    print(x)               # 1, 2, 3

5. Iterable vs Iterator — the difference

iterableiterator
ReusableOK (like a list)One-time use (exhausted)
Methods__iter____iter__ + __next__
Exampleslist, str, dictiter(lst), generator

One-line summary

Iterable = loopable / Iterator = next()-able. Every generator is an iterator. Every iterator is an iterable.

🐍 Try it out — iterator — run it yourself

Run the concepts above as actual code. The fastest way to learn is to change the values yourself and verify how things behave firsthand.
✏️ Python 코드
📟 Console output
▶ Press the Run button
🐍 Real Python via Pyodide — first run takes 3–5s to load

🤖 Try asking AI like this

Knowing the concepts from this lesson lets you give AI specific instructions. Instead of a vague "fix this," you make requests with vocabulary — that is the starting point for saving tokens.

  • "Apply the iterator vs iterable concept to this Python code"
  • "Add type hints and pytest unit tests to this code"
  • "Check this iterator vs iterable code for PEP 8 violations"

Why this reduces tokens

Without knowing the concepts, even after receiving an AI response you have to ask "what does that mean?" again. That follow-up question is what eats your tokens. Learn the concept once and the conversation ends in a single exchange.

Iterator vs Iterable - Python