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

Lesson 02

Python iter() and next() — Iterate One Item at a Time

Use iter() and next() to walk an iterable one item at a time, catch StopIteration, and see why iterators skip indexing and slicing.

Intermediate30 min

![iter and next one item at a time](data:image/svg+xml;charset=utf-8,%3Csvg%20font-family%3D%27ui-sans-serif%2C-apple-system%2Csystem-ui%2CArial%27%20viewBox%3D%270%200%20640%20220%27%20width%3D%27100%25%27%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%3E%20%3Cdefs%3E%20%3ClinearGradient%20id%3D%27igdit%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%23igdit%29%27%2F%3E%20%3Crect%20x%3D%2736%27%20y%3D%2736%27%20width%3D%27320%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%2770%27%20fill%3D%27%23C4B5FD%27%20font-size%3D%2712%27%20font-weight%3D%27bold%27%3EITERATORS%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27104%27%20fill%3D%27%23FFFFFF%27%20font-size%3D%2722%27%20font-weight%3D%27bold%27%3Eiter%28%29%20%2B%20next%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27128%27%20fill%3D%27%23E2E8F0%27%20font-size%3D%2712%27%3Eone%20item%20on%20demand%3C%2Ftext%3E%20%3Ctext%20x%3D%2752%27%20y%3D%27152%27%20fill%3D%27%23A5B4FC%27%20font-size%3D%2711%27%3EPlayground%20%2B%20quiz%3C%2Ftext%3E%20%3Crect%20x%3D%27392%27%20y%3D%2748%27%20width%3D%27212%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%27412%27%20y%3D%2778%27%20fill%3D%27%23FDE68A%27%20font-size%3D%2711%27%20font-family%3D%27Courier%20New%27%3Eiter%28s%29%3C%2Ftext%3E%3Ctext%20x%3D%27412%27%20y%3D%27100%27%20fill%3D%27%23BBF7D0%27%20font-size%3D%2711%27%20font-family%3D%27Courier%20New%27%3Enext%28it%29%3C%2Ftext%3E%3Ctext%20x%3D%27412%27%20y%3D%27122%27%20fill%3D%27%23E0F2FE%27%20font-size%3D%2711%27%20font-family%3D%27Courier%20New%27%3EStopIteration%3C%2Ftext%3E%3Ctext%20x%3D%27412%27%20y%3D%27144%27%20fill%3D%27%23FBCFE8%27%20font-size%3D%2711%27%20font-family%3D%27Courier%20New%27%3Eno%20slices%3C%2Ftext%3E%20%3C%2Fsvg%3E

What you will learn

  • Why iterators exist: do not load the whole dataset
  • iter(iterable) builds an iterator object
  • next(iterator) returns the next value, or raises StopIteration
  • No indexing or slicing on the iterator — values arrive on demand

Functions and special functions already looped lists. This lesson focuses on iterators: Netflix analogy, exam papers, iter / next.

![Netflix sends one title when you ask](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%20340%20180%27%3E%20%3Cdefs%3E%20%3Cmarker%20id%3D%27igditArr%27%20markerWidth%3D%2710%27%20markerHeight%3D%2710%27%20refX%3D%275%27%20refY%3D%275%27%20orient%3D%27auto%27%3E%20%3Cpath%20d%3D%27M0%2C0%20L10%2C5%20L0%2C10%20Z%27%20fill%3D%27%2364748B%27%2F%3E%20%3C%2Fmarker%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%27340%27%20height%3D%27180%27%20rx%3D%2716%27%20fill%3D%27%23FFFFFF%27%20stroke%3D%27%23E5E7EB%27%2F%3E%20%3Crect%20x%3D%2720%27%20y%3D%2720%27%20width%3D%27300%27%20height%3D%2740%27%20rx%3D%2710%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2736%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EAll%2010000%20movies%20at%20once%3C%2Ftext%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2750%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%237F1D1D%27%3EMemory%20overload%3C%2Ftext%3E%20%3Cline%20x1%3D%27170%27%20y1%3D%2768%27%20x2%3D%27170%27%20y2%3D%2786%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%274%204%27%20marker-end%3D%27url%28%23igditArr%29%27%2F%3E%20%3Crect%20x%3D%2720%27%20y%3D%2790%27%20width%3D%27300%27%20height%3D%2770%27%20rx%3D%2710%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%27112%27%20font-size%3D%2710%27%20font-weight%3D%27bold%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3EIterator%3A%20one%20item%20when%20asked%3C%2Ftext%3E%20%3Ctext%20x%3D%27170%27%20y%3D%27132%27%20font-size%3D%279%27%20text-anchor%3D%27middle%27%20fill%3D%27%23065F46%27%3Enext%28%29%20then%20next%28%29%20then%20next%28%29%3C%2Ftext%3E%20%3Ctext%20x%3D%27170%27%20y%3D%27148%27%20font-size%3D%278%27%20text-anchor%3D%27middle%27%20fill%3D%27%23166534%27%3ELike%20Netflix%20sending%20one%20title%3C%2Ftext%3E%20%3C%2Fsvg%3E

Why iterators

If you have a huge file or list, putting every item in memory at once can exhaust RAM. An iterator yields the next bit when you call next().

Netflix does not download 10,000 titles before the first play. You receive one item at a time — not the whole collection at once.

python
it = iter(["Paper1", "Paper2", "Paper3"])
print(next(it))  # Paper1
print(next(it))  # Paper2
print(next(it))  # Paper3

iter() and next()

![iterable to iter to next to StopIteration](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%20340%20200%27%3E%20%3Cdefs%3E%20%3Cmarker%20id%3D%27igditFlow%27%20markerWidth%3D%2710%27%20markerHeight%3D%2710%27%20refX%3D%275%27%20refY%3D%275%27%20orient%3D%27auto%27%3E%20%3Cpath%20d%3D%27M0%2C0%20L10%2C5%20L0%2C10%20Z%27%20fill%3D%27%2364748B%27%2F%3E%20%3C%2Fmarker%3E%20%3C%2Fdefs%3E%20%3Crect%20width%3D%27340%27%20height%3D%27200%27%20rx%3D%2716%27%20fill%3D%27%23F8FAFC%27%2F%3E%20%3Crect%20x%3D%2750%27%20y%3D%2716%27%20width%3D%27240%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23E0F2FE%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2736%27%20font-size%3D%2710%27%20text-anchor%3D%27middle%27%20fill%3D%27%231D4ED8%27%3EIterable%20PYTHON%3C%2Ftext%3E%20%3Cline%20x1%3D%27170%27%20y1%3D%2748%27%20x2%3D%27170%27%20y2%3D%2764%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%274%204%27%20marker-end%3D%27url%28%23igditFlow%29%27%2F%3E%20%3Crect%20x%3D%2750%27%20y%3D%2764%27%20width%3D%27240%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23EDE9FE%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%2784%27%20font-size%3D%2710%27%20text-anchor%3D%27middle%27%20fill%3D%27%235B21B6%27%3Eiter%28%29%20to%20iterator%3C%2Ftext%3E%20%3Cline%20x1%3D%27170%27%20y1%3D%2796%27%20x2%3D%27170%27%20y2%3D%27112%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%274%204%27%20marker-end%3D%27url%28%23igditFlow%29%27%2F%3E%20%3Crect%20x%3D%2750%27%20y%3D%27112%27%20width%3D%27240%27%20height%3D%2732%27%20rx%3D%278%27%20fill%3D%27%23D1FAE5%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%27132%27%20font-size%3D%2710%27%20text-anchor%3D%27middle%27%20fill%3D%27%23047857%27%3Enext%28%29%20one%20character%3C%2Ftext%3E%20%3Cline%20x1%3D%27170%27%20y1%3D%27144%27%20x2%3D%27170%27%20y2%3D%27160%27%20stroke%3D%27%2364748B%27%20stroke-dasharray%3D%274%204%27%20marker-end%3D%27url%28%23igditFlow%29%27%2F%3E%20%3Crect%20x%3D%2750%27%20y%3D%27160%27%20width%3D%27240%27%20height%3D%2728%27%20rx%3D%278%27%20fill%3D%27%23FEE2E2%27%2F%3E%20%3Ctext%20x%3D%27170%27%20y%3D%27178%27%20font-size%3D%2710%27%20text-anchor%3D%27middle%27%20fill%3D%27%23991B1B%27%3EStopIteration%20when%20empty%3C%2Ftext%3E%20%3C%2Fsvg%3E

  • iter() converts an iterable (str, list, tuple, set, dict, file, …) into an iterator
  • next() obtains the next element
  • When nothing remains, Python raises StopIteration
python
s = "PYTHON"
itobj = iter(s)
while True:
    try:
        item = next(itobj)
        print(item)
    except StopIteration:
        break

Some examples mistakenly call next(s) on the original string. After itobj = iter(s) you must call next(itobj). Calling next(s) on a str raises TypeError because a string is iterable, not an iterator.

Tuple demo:

python
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))

Rules

  • Iterators supply on demand
  • No itobj[0] or itobj[1:3]
  • A for loop calls iter / next for you and swallows StopIteration

Interview cheat sheet

Topic Answer
Convert iter(obj)
Step next(it)
End StopIteration
Indexing Not on the iterator

Practice in the playground

  1. Call next(itobj) until StopIteration, then catch it.
  2. Try itobj[0] and read the error.
  3. Replace the while with for item in iter(s):.

Next: iterators on list, tuple, str, set, dict.

Loading playground…

Check your understanding

iter() and next() quiz

iter(s) when s is 'PYTHON' returns:

1 / 10