Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 03

Iterate list, tuple, str, set, and dict — Iterator Type Names

Map list, tuple, str, set, and dict to everyday stories—basket, queue, letters, lottery, keys—then match Python iterator type names.

Intermediate35 min

![five collections five iterator types visual memory map hero](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20640%20220%27%3E%20%3Cdefs%3E%20%3ClinearGradient%20id%3D%27igdcol2h%27%20x1%3D%270%27%20y1%3D%270%27%20x2%3D%271%27%20y2%3D%271%27%3E%20%3Cstop%20offset%3D%270%25%27%20stop-color%3D%27%237C3AED%27%2F%3E%20%3Cstop%20offset%3D%2755%25%27%20stop-color%3D%27%232563EB%27%2F%3E%20%3Cstop%20offset%3D%27100%25%27%20stop-color%3D%27%230F172A%27%2F%3E%20%3C%2FlinearGradient%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%27640%27%20height%3D%27220%27%20rx%3D%2720%27%20fill%3D%27url%28%23igdcol2h%29%27%2F%3E%20%3Crect%20x%3D%2736%27%20y%3D%2736%27%20width%3D%27360%27%20height%3D%27148%27%20rx%3D%2714%27%20fill%3D%27%230F172A%27%20opacity%3D%270.45%27%2F%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2768%27%20fill%3D%27%23C4B5FD%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%3ELESSON%2003%20%C2%B7%20VISUAL%20MEMORY%20MAP%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%2798%27%20fill%3D%27%23FFFFFF%27%20font-size%3D%2720%27%20font-weight%3D%27bold%27%3E5%20Collections%20to%205%20Iterator%20Types%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27122%27%20fill%3D%27%23E2E8F0%27%20font-size%3D%2712%27%3EBasket%20%C2%B7%20Queue%20%C2%B7%20Letters%20%C2%B7%20Lottery%20%C2%B7%20Hotel%20keys%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27148%27%20fill%3D%27%23A5B4FC%27%20font-size%3D%2711%27%3Elist%20through%20dict%20iterators%3C%2Ftext%3E%20%3Crect%20x%3D%27420%27%20y%3D%2748%27%20width%3D%27184%27%20height%3D%27124%27%20rx%3D%2712%27%20fill%3D%27%23FFFFFF%27%20opacity%3D%270.12%27%2F%3E%20%3Ctext%20x%3D%27440%27%20y%3D%2778%27%20fill%3D%27%23FDE68A%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Elist_iterator%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%2798%27%20fill%3D%27%23BBF7D0%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Etuple_iterator%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27118%27%20fill%3D%27%23E0F2FE%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Eset_iterator%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27138%27%20fill%3D%27%23FBCFE8%27%20font-size%3D%2710%27%20font-family%3D%27Courier%20New%27%3Edict_keyiterator%3C%2Ftext%3E%20%3Ctext%20x%3D%27440%27%20y%3D%27158%27%20fill%3D%27%23FECACA%27%20font-size%3D%2710%27%3Eint%20not%20iterable%3C%2Ftext%3E%20%3C%2Fsvg%3E

5 Collections to 5 Iterator Types

Instead of memorizing list_iterator, tuple_iterator, and the rest, connect each collection to something you use every day — then learn the real type names.

Every iterable becomes an iterator

Think of iter() as turning a collection into a Next button that reveals one item at a time.

![collection iter iterator next one value flow](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2084%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2784%27%20rx%3D%2712%27%20fill%3D%27%231E293B%27%2F%3E%20%3Crect%20x%3D%2710%27%20y%3D%2722%27%20width%3D%2768%27%20height%3D%2740%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%2F%3E%20%3Ctext%20x%3D%2744%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3ECollection%3C%2Ftext%3E%20%3Ctext%20x%3D%2744%27%20y%3D%2748%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%231E40AF%27%3Elist%2C%20str...%3C%2Ftext%3E%20%3Cpath%20d%3D%27M78%2042%20H110%27%20stroke%3D%27%23FFFFFF%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27106%2C38%20114%2C42%20106%2C46%27%20fill%3D%27%23FFFFFF%27%2F%3E%20%3Crect%20x%3D%27114%27%20y%3D%2722%27%20width%3D%2744%27%20height%3D%2740%27%20rx%3D%278%27%20fill%3D%27%23C4B5FD%27%2F%3E%20%3Ctext%20x%3D%27136%27%20y%3D%2744%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%234C1D95%27%3Eiter%28%29%3C%2Ftext%3E%20%3Cpath%20d%3D%27M158%2042%20H190%27%20stroke%3D%27%23FFFFFF%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27186%2C38%20194%2C42%20186%2C46%27%20fill%3D%27%23FFFFFF%27%2F%3E%20%3Crect%20x%3D%27194%27%20y%3D%2722%27%20width%3D%2752%27%20height%3D%2740%27%20rx%3D%278%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%27220%27%20y%3D%2738%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EIterator%3C%2Ftext%3E%20%3Ctext%20x%3D%27220%27%20y%3D%2748%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Enext%28%29%3C%2Ftext%3E%20%3Cpath%20d%3D%27M246%2042%20H278%27%20stroke%3D%27%23FFFFFF%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27274%2C38%20282%2C42%20274%2C46%27%20fill%3D%27%23FFFFFF%27%2F%3E%20%3Crect%20x%3D%27282%27%20y%3D%2722%27%20width%3D%2728%27%20height%3D%2740%27%20rx%3D%278%27%20fill%3D%27%23FDE68A%27%2F%3E%20%3Ctext%20x%3D%27296%27%20y%3D%2744%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3E1%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2776%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%2394A3B8%27%3ECollection%20to%20iter%20to%20Iterator%20to%20next%20to%20one%20value%3C%2Ftext%3E%20%3C%2Fsvg%3E

Collection → iter() → Iterator → next() → one value

What you will learn

  • Five everyday stories mapped to list, tuple, str, set, and dict iterators
  • Type names for list, tuple, str, set, and dict iterators
  • Why iter(123445) raises TypeError
  • Dict iterators yield keys — use x.get(key) for values
  • File lines as an iterator (StringIO in the playground)

1. List = Shopping Basket

A basket already holds many items. You pick one product each time — you do not eat everything at once.

![shopping basket pick apple milk bread one at a time](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2064%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2764%27%20rx%3D%2710%27%20fill%3D%27%23EFF6FF%27%2F%3E%20%3Ccircle%20cx%3D%2754%27%20cy%3D%2732%27%20r%3D%2718%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%2754%27%20y%3D%2736%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EAp%3C%2Ftext%3E%20%3Cpath%20d%3D%27M72%2032%20H142%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27136%2C28%20144%2C32%20136%2C36%27%20fill%3D%27%2364748B%27%2F%3E%20%3Ccircle%20cx%3D%27160%27%20cy%3D%2732%27%20r%3D%2718%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2736%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EMk%3C%2Ftext%3E%20%3Cpath%20d%3D%27M178%2032%20H248%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%273%203%27%2F%3E%20%3Cpolygon%20points%3D%27242%2C28%20250%2C32%20242%2C36%27%20fill%3D%27%2364748B%27%2F%3E%20%3Ccircle%20cx%3D%27266%27%20cy%3D%2732%27%20r%3D%2718%27%20fill%3D%27%23DBEAFE%27%20stroke%3D%27%232563EB%27%2F%3E%20%3Ctext%20x%3D%27266%27%20y%3D%2736%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EBr%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story example:

python
basket = ["Apple", "Milk", "Bread"]
it = iter(basket)
print(next(it))   # Apple
print(next(it))   # Milk

Iterator type: list_iterator

Mixed-type list — while + next:

python
x = [10, "RS", 23.45, 3+4j, True, False]
itr = iter(x)
print(type(itr))  # <class 'list_iterator'>
while True:
    try:
        print(next(itr))
    except StopIteration:
        break

2. Tuple = People in a Train Queue

A queue has a fixed order. People do not swap places — you simply call the next passenger.

![train queue fixed order A B C next passenger](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2072%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2772%27%20rx%3D%2712%27%20fill%3D%27%23ECFDF5%27%2F%3E%20%3Ctext%20x%3D%2718%27%20y%3D%2720%27%20font-size%3D%278%27%20font-weight%3D%27bold%27%20fill%3D%27%23047857%27%3ENext%3C%2Ftext%3E%20%3Ccircle%20cx%3D%2778%27%20cy%3D%2740%27%20r%3D%2716%27%20fill%3D%27%23A7F3D0%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%2778%27%20y%3D%2744%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EA%3C%2Ftext%3E%20%3Ccircle%20cx%3D%27148%27%20cy%3D%2740%27%20r%3D%2716%27%20fill%3D%27%23A7F3D0%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27148%27%20y%3D%2744%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EB%3C%2Ftext%3E%20%3Ccircle%20cx%3D%27218%27%20cy%3D%2740%27%20r%3D%2716%27%20fill%3D%27%23A7F3D0%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27218%27%20y%3D%2744%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3EC%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story example:

python
people = ("A", "B", "C")
it = iter(people)
print(next(it))   # A

Iterator type: tuple_iterator

Tuple example:

python
x = (10, "RS", True)
itr = iter(x)
print(type(itr))  # <class 'tuple_iterator'>
print(next(itr), next(itr), next(itr))

3. String = Reading a Word

When you read PYTHON, you naturally take one letter at a time.

![PYTHON letters one character at a time](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2072%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2772%27%20rx%3D%2712%27%20fill%3D%27%23F5F3FF%27%2F%3E%20%3Crect%20x%3D%2710%27%20y%3D%2720%27%20width%3D%2734%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%2727%27%20y%3D%2740%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EP%3C%2Ftext%3E%20%3Crect%20x%3D%2760%27%20y%3D%2720%27%20width%3D%2734%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%2777%27%20y%3D%2740%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EY%3C%2Ftext%3E%20%3Crect%20x%3D%27110%27%20y%3D%2720%27%20width%3D%2734%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%27127%27%20y%3D%2740%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3ET%3C%2Ftext%3E%20%3Crect%20x%3D%27160%27%20y%3D%2720%27%20width%3D%2734%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%27177%27%20y%3D%2740%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EH%3C%2Ftext%3E%20%3Crect%20x%3D%27210%27%20y%3D%2720%27%20width%3D%2734%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%27227%27%20y%3D%2740%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EO%3C%2Ftext%3E%20%3Crect%20x%3D%27260%27%20y%3D%2720%27%20width%3D%2734%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23DDD6FE%27%20stroke%3D%27%237C3AED%27%2F%3E%20%3Ctext%20x%3D%27277%27%20y%3D%2740%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3EN%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story example:

python
word = "PYTHON"
for ch in iter(word):
    print(ch)

Iterator type: str_iterator (exact name may vary slightly by Python version)

The same for val in itr pattern works on "PYTHON".

4. Set = Lottery Ball Machine

Lottery balls come out one by one — but the order is unpredictable.

![lottery balls random order 10 23 45](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2084%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2784%27%20rx%3D%2712%27%20fill%3D%27%23FFFBEB%27%2F%3E%20%3Ccircle%20cx%3D%2760%27%20cy%3D%2736%27%20r%3D%2720%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%2760%27%20y%3D%2740%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3E23%3C%2Ftext%3E%20%3Ccircle%20cx%3D%27160%27%20cy%3D%2736%27%20r%3D%2720%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2740%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3E10%3C%2Ftext%3E%20%3Ccircle%20cx%3D%27260%27%20cy%3D%2736%27%20r%3D%2720%27%20fill%3D%27%23FDE68A%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Ctext%20x%3D%27260%27%20y%3D%2740%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3E45%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%2768%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%2392400E%27%3ERandom%20order%20%E2%80%94%20do%20not%20depend%20on%20print%20order%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story example:

python
nums = {10, 23, 45}
for n in iter(nums):
    print(n)

Important: never depend on the printed order.

Iterator type: set_iterator

Set example:

python
x = {10, "RS", 23.45, 3+4j, True, False}
itr = iter(x)
print(type(itr))  # set_iterator
for val in itr:
    print(val)

5. Dictionary = Hotel Room Keys

The receptionist hands you room numbers first. You open each room to see the guest name.

![hotel keys 101 102 103 map to guest names](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20120%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27120%27%20rx%3D%2714%27%20fill%3D%27%23FEF2F2%27%2F%3E%20%3Crect%20x%3D%2716%27%20y%3D%2714%27%20width%3D%2756%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%23FCA5A5%27%20stroke%3D%27%23DC2626%27%2F%3E%20%3Ctext%20x%3D%2744%27%20y%3D%2732%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3E101%3C%2Ftext%3E%20%3Crect%20x%3D%278%27%20y%3D%2758%27%20width%3D%2772%27%20height%3D%2734%27%20rx%3D%276%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%2744%27%20y%3D%2778%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3ERahul%3C%2Ftext%3E%20%3Crect%20x%3D%27116%27%20y%3D%2714%27%20width%3D%2756%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%23FCA5A5%27%20stroke%3D%27%23DC2626%27%2F%3E%20%3Ctext%20x%3D%27144%27%20y%3D%2732%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3E102%3C%2Ftext%3E%20%3Crect%20x%3D%27108%27%20y%3D%2758%27%20width%3D%2772%27%20height%3D%2734%27%20rx%3D%276%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%27144%27%20y%3D%2778%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EPriya%3C%2Ftext%3E%20%3Crect%20x%3D%27216%27%20y%3D%2714%27%20width%3D%2756%27%20height%3D%2728%27%20rx%3D%276%27%20fill%3D%27%23FCA5A5%27%20stroke%3D%27%23DC2626%27%2F%3E%20%3Ctext%20x%3D%27244%27%20y%3D%2732%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3E103%3C%2Ftext%3E%20%3Crect%20x%3D%27208%27%20y%3D%2758%27%20width%3D%2772%27%20height%3D%2734%27%20rx%3D%276%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%27244%27%20y%3D%2778%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3ESwathi%3C%2Ftext%3E%20%3Ctext%20x%3D%27160%27%20y%3D%27108%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EKey%20first%2C%20guest%20name%20with%20get%28%29%3C%2Ftext%3E%20%3C%2Fsvg%3E

Story example:

python
rooms = {101: "Rahul", 102: "Priya", 103: "Swathi"}
for m in iter(rooms):
    print(m, rooms.get(m))

Key ideas:

  • iter(dict) → gives keys
  • rooms.get(key) → gives values

Iterator type: dict_keyiterator (name may vary by version)

Dict example:

python
x = {10: "Python", 20: "C", 30: "Java", 40: "C++"}
itr = iter(x)
for val in itr:
    print("{}	{}".format(val, x.get(val)))

Why does iter(12345) fail?

A number is like a single brick. You cannot walk through it because it is not a sequence.

![int single brick not iterable str is character sequence](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%2090%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%2790%27%20rx%3D%2712%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2724%27%20y%3D%2718%27%20width%3D%27120%27%20height%3D%2754%27%20rx%3D%2710%27%20fill%3D%27%23FEE2E2%27%20stroke%3D%27%23EF4444%27%2F%3E%20%3Ctext%20x%3D%2784%27%20y%3D%2738%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3Eint%3C%2Ftext%3E%20%3Ctext%20x%3D%2784%27%20y%3D%2752%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3ESingle%20brick%3C%2Ftext%3E%20%3Ctext%20x%3D%2784%27%20y%3D%2764%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3ENot%20iterable%3C%2Ftext%3E%20%3Crect%20x%3D%27176%27%20y%3D%2718%27%20width%3D%27120%27%20height%3D%2754%27%20rx%3D%2710%27%20fill%3D%27%23D1FAE5%27%20stroke%3D%27%23059669%27%2F%3E%20%3Ctext%20x%3D%27236%27%20y%3D%2738%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Estr%3C%2Ftext%3E%20%3Ctext%20x%3D%27236%27%20y%3D%2752%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3ESequence%20of%20chars%3C%2Ftext%3E%20%3Ctext%20x%3D%27236%27%20y%3D%2764%27%20font-size%3D%277%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Eiter%28%29%20works%3C%2Ftext%3E%20%3C%2Fsvg%3E

python
iter(123445)
# TypeError: 'int' object is not iterable

If you meant digits as characters, cast first:

python
for d in str(123445):
    print(d)

Remember: int is not iterable. The playground demonstrates iter(123445) raising TypeError.

File iterator = Reading a Notebook

A text file behaves like turning notebook pages — you read one line at a time. The playground uses StringIO instead of a file path on disk.

![notebook one line per page Swathi Rahul Priya](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20100%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27100%27%20rx%3D%2712%27%20fill%3D%27%23FFFBEB%27%20stroke%3D%27%23E5E7EB%27%2F%3E%20%3Crect%20x%3D%2740%27%20y%3D%2716%27%20width%3D%27200%27%20height%3D%2768%27%20rx%3D%278%27%20fill%3D%27%23FFFFFF%27%20stroke%3D%27%23D97706%27%2F%3E%20%3Cline%20x1%3D%2770%27%20y1%3D%2716%27%20x2%3D%2770%27%20y2%3D%2784%27%20stroke%3D%27%23FDE68A%27%20stroke-width%3D%273%27%2F%3E%20%3Ctext%20x%3D%27120%27%20y%3D%2738%27%20font-size%3D%279%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3ESwathi%3C%2Ftext%3E%20%3Cline%20x1%3D%2780%27%20y1%3D%2748%27%20x2%3D%27200%27%20y2%3D%2748%27%20stroke%3D%27%23E5E7EB%27%2F%3E%20%3Ctext%20x%3D%27120%27%20y%3D%2762%27%20font-size%3D%279%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3ERahul%3C%2Ftext%3E%20%3Cline%20x1%3D%2780%27%20y1%3D%2772%27%20x2%3D%27200%27%20y2%3D%2772%27%20stroke%3D%27%23E5E7EB%27%2F%3E%20%3Ctext%20x%3D%27120%27%20y%3D%2786%27%20font-size%3D%279%27%20text-anchor%3D%27middle%27%20fill%3D%27%23374151%27%3EPriya%3C%2Ftext%3E%20%3Ctext%20x%3D%27260%27%20y%3D%2750%27%20font-size%3D%278%27%20fill%3D%27%2392400E%27%3Eone%20line%3C%2Ftext%3E%20%3Ctext%20x%3D%27260%27%20y%3D%2762%27%20font-size%3D%278%27%20fill%3D%27%2392400E%27%3Eper%20page%3C%2Ftext%3E%20%3C%2Fsvg%3E

python
from io import StringIO

file = StringIO("Swathi\nRahul\nPriya\n")
for line in file:
    print(line.strip())

Output:

text
Swathi
Rahul
Priya

Run the full mix — list, str, tuple, set, dict, and iter(123445) — in the playground below.

Interview memory table

Collection Think of... Iterator type
List Shopping basket list_iterator
Tuple Train queue tuple_iterator
String Reading letters str_iterator
Set Lottery balls set_iterator
Dictionary Hotel room keys dict_keyiterator
Integer Single brick Not iterable

One-minute visual recap

![list basket tuple queue string letters set lottery dict keys recap](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%20320%20200%27%3E%20%3Crect%20width%3D%27320%27%20height%3D%27200%27%20rx%3D%2716%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2718%27%20y%3D%2714%27%20width%3D%27284%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23DBEAFE%27%2F%3E%20%3Ctext%20x%3D%2730%27%20y%3D%2730%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%231D4ED8%27%3EList%3C%2Ftext%3E%20%3Ctext%20x%3D%27290%27%20y%3D%2730%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%231D4ED8%27%3EShopping%20basket%3C%2Ftext%3E%20%3Crect%20x%3D%2718%27%20y%3D%2750%27%20width%3D%27284%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%2730%27%20y%3D%2766%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%23047857%27%3ETuple%3C%2Ftext%3E%20%3Ctext%20x%3D%27290%27%20y%3D%2766%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%23047857%27%3ETrain%20queue%3C%2Ftext%3E%20%3Crect%20x%3D%2718%27%20y%3D%2786%27%20width%3D%27284%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23EDE9FE%27%2F%3E%20%3Ctext%20x%3D%2730%27%20y%3D%27102%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%236D28D9%27%3EString%3C%2Ftext%3E%20%3Ctext%20x%3D%27290%27%20y%3D%27102%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%236D28D9%27%3EReading%20letters%3C%2Ftext%3E%20%3Crect%20x%3D%2718%27%20y%3D%27122%27%20width%3D%27284%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23FDE68A%27%2F%3E%20%3Ctext%20x%3D%2730%27%20y%3D%27138%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%2392400E%27%3ESet%3C%2Ftext%3E%20%3Ctext%20x%3D%27290%27%20y%3D%27138%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%2392400E%27%3ELottery%20balls%3C%2Ftext%3E%20%3Crect%20x%3D%2718%27%20y%3D%27158%27%20width%3D%27284%27%20height%3D%2726%27%20rx%3D%278%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%2730%27%20y%3D%27174%27%20font-size%3D%279%27%20font-weight%3D%27bold%27%20fill%3D%27%23B91C1C%27%3EDict%3C%2Ftext%3E%20%3Ctext%20x%3D%27290%27%20y%3D%27174%27%20font-size%3D%278%27%20text-anchor%3D%27end%27%20fill%3D%27%23B91C1C%27%3EHotel%20room%20keys%3C%2Ftext%3E%20%3C%2Fsvg%3E

Golden rule

iter() works only on iterables — list, tuple, string, set, dictionary, file. It does not work on an int; that raises TypeError.

Next: Generators and yield.

Loading playground…

Check your understanding

Collection iterators quiz

For a list, iter(lst) gives type(itr) as:

1 / 10