👉 Simple Definition:
next() is a built-in Python function used to get the next item from an iterator.
It moves forward one step at a time.
Before next(), you must understand:
An object that can be looped over:
list
tuple
string
dict
set
An object that:
Remembers its position
Returns next value when asked
Raises StopIteration when finished
You create an iterator using:
iter()
next()next(iterator, default_value)
iterator → Required
default_value → Optional (prevents error)
numbers = [10, 20, 30]
it = iter(numbers)
print(next(it)) # 10
print(next(it)) # 20
print(next(it)) # 30
print(next(it)) # ❌ Error
10
20
30
StopIteration
After last element → Python raises:
StopIteration
numbers = [10, 20]
it = iter(numbers)
print(next(it)) # 10
print(next(it)) # 20
print(next(it, "Done")) # Done
Now instead of error → it returns "Done"
When you do:
next(it)
Python internally calls:
it.__next__()
So:
next(it)
is same as:
it.__next__()
But always use next() (cleaner and safer).
for Loop Uses next()This:
for num in numbers:
print(num)
Internally works like this:
it = iter(numbers)
while True:
try:
num = next(it)
print(num)
except StopIteration:
break
👉 So for loop is secretly calling next() repeatedly!
def count_up_to(n):
i = 1
while i <= n:
yield i
i += 1
Now:
counter = count_up_to(3)
print(next(counter)) # 1
print(next(counter)) # 2
print(next(counter)) # 3
print(next(counter)) # StopIteration
Generators do NOT store all values in memory.
They produce values on demand using next().
Very important in:
Data pipelines
LLM streaming
RAG workflows
Async systems
You previously saw something like:
tool = next((t for t in self.mcp_tools if t.name == "get_product_info"), None)
(t for t in self.mcp_tools if t.name == "get_product_info")
This is a generator expression.
It produces tools one by one.
Then:
next(generator, None)
Means:
Give me the FIRST matching tool
If not found → return None
So it safely fetches one item without looping manually.
Very powerful pattern 👌
class MyCounter:
def __init__(self, max):
self.max = max
self.current = 1
def __iter__(self):
return self
def __next__(self):
if self.current > self.max:
raise StopIteration
value = self.current
self.current += 1
return value
Now:
counter = MyCounter(3)
print(next(counter)) # 1
print(next(counter)) # 2
print(next(counter)) # 3
| Concept | Meaning |
|---|---|
| iterable | Can be looped |
| iterator | Remembers position |
| iter() | Converts iterable to iterator |
| next() | Gets next item |
| StopIteration | Raised when finished |
| default parameter | Prevents error |
Imagine:
iter() → creates a remote control
next() → presses "Next" button
When no channels left → StopIteration
next() as Data Scientist / AI EngineerFetch first matching record
Streaming APIs
Generators for large datasets
Lazy loading
Tool selection in agents
RAG pipelines
LangChain internals
❌ Calling next() directly on list:
next([1,2,3]) # ERROR
✔ Correct:
next(iter([1,2,3]))
Get first even number:
nums = [1, 3, 5, 8, 10]
first_even = next((n for n in nums if n % 2 == 0), None)
print(first_even) # 8
Efficient because:
Stops at first match
Doesn’t scan entire list
next() =
👉 Manual control over iteration
👉 Used internally by loops
👉 Critical for generators
👉 Very powerful in real-world Python systems