๐Ÿฒ Part 3: Advanced Iteration Tricks

Chefs, Sous-chefs, and Lazy Pipelines

By now youโ€™ve met:

The buffet (iterable: the source of food ๐Ÿฑ)
The waiter (iterator: walks forward ๐Ÿšถโ€โ™‚๏ธ)
The autopilot waiter (generator: powered by yield ๐Ÿค–)

But real restaurants need teams sous-chefs, assista…


This content originally appeared on DEV Community and was authored by Anik Sikder

Chefs, Sous-chefs, and Lazy Pipelines

By now youโ€™ve met:

  • The buffet (iterable: the source of food ๐Ÿฑ)
  • The waiter (iterator: walks forward ๐Ÿšถโ€โ™‚๏ธ)
  • The autopilot waiter (generator: powered by yield ๐Ÿค–)

But real restaurants need teams sous-chefs, assistants, conveyor belts, and clever tricks to keep food moving.

Today we explore advanced iteration: itertools, yield from, generator delegation, cloning with tee, and lazy pipelines.
And this time, weโ€™ll tie it to real-world mini-projects so you see the practical magic.

๐Ÿด 1. itertools โ†’ Kitchen Gadgets

Think of itertools as Pythonโ€™s drawer of restaurant gadgets simple tools that make iteration effortless.

Fun Example: Endless Breadsticks

import itertools

for bread in itertools.count(1):  # waiter counts forever
    if bread > 5:
        break
    print(bread)

๐Ÿž Output:

1
2
3
4
5

Behind the curtain:

  • count is just a tiny iterator with a counter.
  • Each next() increments, no giant list needed.

๐ŸŸ Real-World Mini-Project: Streaming Logs

import itertools

def read_logs(filename):
    with open(filename) as f:
        for line in f:
            yield line.strip()

# Look only at the first 5 errors lazily
logs = read_logs("server.log")
errors = (l for l in logs if "ERROR" in l)
for e in itertools.islice(errors, 5):
    print("๐Ÿšจ", e)

๐Ÿ‘‰ You can process gigantic logs without loading them all into memory.

๐Ÿฅก 2. yield from โ†’ Waiter Delegation

When a waiter has too many trays, he calls an assistant:
โ€œHey intern, serve these dishes for me.โ€

Example:

def menu():
    yield 1
    yield 2
    yield from [3, 4]  # delegation

Output โ†’ [1, 2, 3, 4].

Behind the curtain:

  • yield from iterable expands into a for loop.
  • It even forwards exceptions and return values.

๐Ÿ• Real-World Mini-Project: Nested Config Loader

def load_base():
    yield "db=sqlite"
    yield "timeout=30"

def load_dev():
    yield from load_base()
    yield "debug=true"

print(list(load_dev()))
# ['db=sqlite', 'timeout=30', 'debug=true']

๐Ÿ‘‰ yield from = clean delegation of tasks (like dev configs building on base configs).

๐Ÿ 3. Generator Delegation (Sous-Chefs)

Restaurants donโ€™t have one chef for all courses. They delegate: appetizers, mains, desserts.

def appetizer():
    yield "salad ๐Ÿฅ—"
    yield "soup ๐Ÿœ"

def main_course():
    yield "steak ๐Ÿฅฉ"
    yield "pasta ๐Ÿ"

def dessert():
    yield "cake ๐Ÿฐ"

def full_meal():
    yield from appetizer()
    yield from main_course()
    yield from dessert()

print(list(full_meal()))

Output โ†’ ['salad ๐Ÿฅ—','soup ๐Ÿœ','steak ๐Ÿฅฉ','pasta ๐Ÿ','cake ๐Ÿฐ'].

๐Ÿœ Real-World Mini-Project: File Processing Pipeline

def read_lines(path):
    with open(path) as f:
        for line in f:
            yield line

def parse_csv(lines):
    for line in lines:
        yield line.strip().split(",")

def filter_users(rows):
    for row in rows:
        if int(row[2]) > 30:  # age > 30
            yield row

def users_over_30(path):
    yield from filter_users(parse_csv(read_lines(path)))

for u in users_over_30("users.csv"):
    print(u)

๐Ÿ‘‰ This is a real pipeline: one generator hands off to the next like sous-chefs in a line.

๐Ÿฅ‚ 4. itertools.tee โ†’ Cloning Waiters

Normally: one waiter, one trip. If Alice eats, Bob gets an empty plate.

tee = photocopy the waiter so both can eat.

import itertools

it = iter([1, 2, 3])
alice, bob = itertools.tee(it)

print(list(alice))  # [1, 2, 3]
print(list(bob))    # [1, 2, 3]

Behind the curtain:

  • tee buffers values if one consumer lags behind.
  • Watch out: memory grows if consumers are very out of sync.

๐Ÿค Real-World Mini-Project: Dual Consumers (Analytics + Logging)

orders = (f"Order {i}" for i in range(1, 6))

# Clone the stream
chef, cashier = itertools.tee(orders)

print("Chef prepares:", list(chef))
print("Cashier logs:", list(cashier))

๐Ÿ‘‰ Same data stream, used by two different subsystems.

๐Ÿง 5. Lazy Pipelines โ†’ Conveyor Belts of Food

Now the coolest trick: combine everything into a conveyor belt where dishes flow step by step.

import itertools

nums = range(1, 1000000)

pipeline = itertools.islice(
    (n**2 for n in nums if n % 2),  # odd squares
    5
)

print(list(pipeline))  # [1, 9, 25, 49, 81]

Behind the curtain:

  • Each stage is lazy โ†’ nothing happens until next() is called.
  • You can chain infinite data sources safely.

๐Ÿœ Real-World Mini-Project: Live Order Filter

import itertools

def infinite_orders():
    n = 1
    while True:
        yield f"Pizza #{n}"
        n += 1

orders = itertools.islice(
    (o for o in infinite_orders() if "3" not in o),  # skip unlucky 3s
    5
)

print(list(orders))

Output:

['Pizza #1', 'Pizza #2', 'Pizza #4', 'Pizza #5', 'Pizza #6']

๐Ÿ‘‰ Streaming infinite orders, but filtered + capped safely.

๐ŸŽจ ASCII Mental Model

[ Buffet ] โ†’ [ Waiter ] โ†’ [ Gadget ] โ†’ [ Filter ] โ†’ [ Map ] โ†’ [ Eater ]

Think of it as a restaurant conveyor belt each chef only touches one dish at a time, dishes move lazily, no giant tray dumped all at once.

๐ŸŽฌ Wrap-Up

Today we upgraded from one waiter to a whole restaurant team:

  • itertools: kitchen gadgets (count, cycle, chain, islice).
  • yield from: waiter delegation.
  • generator delegation: sous-chefs working in harmony.
  • tee: cloning waiters with buffering.
  • lazy pipelines: conveyor belts of infinite food.

๐Ÿ‘‰ Next up (Part 4): Coroutines waiters who donโ€™t just serve, but can also take your orders mid-service (send, throw, async/await).


This content originally appeared on DEV Community and was authored by Anik Sikder


Print Share Comment Cite Upload Translate Updates
APA

Anik Sikder | Sciencx (2025-09-25T15:00:00+00:00) ๐Ÿฒ Part 3: Advanced Iteration Tricks. Retrieved from https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/

MLA
" » ๐Ÿฒ Part 3: Advanced Iteration Tricks." Anik Sikder | Sciencx - Thursday September 25, 2025, https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/
HARVARD
Anik Sikder | Sciencx Thursday September 25, 2025 » ๐Ÿฒ Part 3: Advanced Iteration Tricks., viewed ,<https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/>
VANCOUVER
Anik Sikder | Sciencx - » ๐Ÿฒ Part 3: Advanced Iteration Tricks. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/
CHICAGO
" » ๐Ÿฒ Part 3: Advanced Iteration Tricks." Anik Sikder | Sciencx - Accessed . https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/
IEEE
" » ๐Ÿฒ Part 3: Advanced Iteration Tricks." Anik Sikder | Sciencx [Online]. Available: https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/. [Accessed: ]
rf:citation
» ๐Ÿฒ Part 3: Advanced Iteration Tricks | Anik Sikder | Sciencx | https://www.scien.cx/2025/09/25/%f0%9f%8d%b2-part-3-advanced-iteration-tricks/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.