🥱 Lazy Processing in Python


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

🔗 The Merging Problem (itertools)

Let's say you have two separate data streams, transaction amount - offline and online . And you need to process them as one continuous flow.

sales_offline = [240, 180]
sales_online = [870, 910]

Standard Python will combine and create a new list. That eats memory (imagine if there are thousands of transactions).

Here comes itertools - which processes it like a stream (step by step). So instead of creating a separate list, itertools (chain) will first take care of sales_offline and then sales_online (lazy processing)

In:
from itertools import chain

sales_overall = chain(sales_offline, sales_online)
for log in sales_overall:
    print(log)
Out:
240
180
870
910

More use cases...

⌛ First load important functions of itertools

In: from itertools import count, cycle, repeat, chain, accumulate

🌱 Grow list dynamically

Log customer ID (stating from 10) as they are generated

In:
c = count(10)
print([next(c) for _ in range(5)])
Out: [10, 11, 12, 13, 14]

🔄 Loop a sequence

In:
cy = cycle(["A", "B"])
print([next(cy) for _ in range(6)])
Out: ['A', 'B', 'A', 'B', 'A', 'B']

🎯 Repeat same values

Example: Use as placeholders if some data is missing

In: print(list(repeat("x", 4))))
Out: ['x', 'x', 'x', 'x']

✨ Add numbers real-time

Example: Total store sale till afternoon

In:
tx = [120, -30, 50, -10]
print(list(accumulate(tx)))
Out: [120, 90, 140, 130]

📈 Monitor highest value

Example: Costliest product sold till now

In:
metrics = [300, 250, 420, 380, 500]
print(list(accumulate(metrics, max)))
Out: [300, 300, 420, 420, 500]

⭐📣 That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. You have a spreadsheet with 500 numbers. Your boss asks: "What do you see?" Most will freeze, scroll, guess. Not you! 3 Python functions to turn raw numbers into answers. No calculator, excel sheets. Just code. Statistics isn't boring math. It's how you make decisions with data. Using just the statistics module we find "center" of any dataset. Any data analyst must know the basics‼️ 🎯 Compute simple...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Last week we introduced numpy. Created bunch of arrays, sliced them and did some math with them. To prepare data for machine learning or any analysis, we need to learn to control their shape. Today - Reshape flat lists into grids. Flatten 2D tables back to 1D arrays. Create arrays filled with zeros, ones, or random numbers. ⏪ Recap: See dimensions first In: import numpy as np sales = np.array([120, 340,...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. TGIF! Everyone is talking about AI. Most people take courses. You're going to build it instead. Let's create a simple sentiment analyzer that reads customer reviews and classifies them as positive or negative. No machine learning libraries—just Python. 🎯 Define list of positive and negative words In: positive_words = ["good", "great", "excellent", "amazing", "love", "best"] negative_words = ["bad",...