List Comprehensions and Generator Expressions#
Comprehensions = 50 Excel formulas β 1 Python line Generators = Analyze 1M rows without crashing
Interview question #1: βWrite this with comprehensionβ
π― Comprehensions = Business Analytics Superpower#
Task |
Excel |
Comprehension |
Lines Saved |
|---|---|---|---|
Filter profits |
10 formulas |
|
50x |
Calculate margins |
20 formulas |
|
100x |
VIP customers |
5 filters |
|
Infinite |
Growth months |
Pivot table |
|
Production |
π Step 1: List Comprehension Mastery#
# 50 LINES β 1 LINE MAGIC (Run this!)
monthly_sales = [25000, 28000, 32000, 12000, 35000, 18000, 42000]
# JUNIOR (10 lines)
profits = []
high_profit_months = []
for sales in monthly_sales:
profit = sales * 0.28 - 8000
profits.append(profit)
if profit > 5000:
high_profit_months.append(profit)
# PRO (2 lines!)
profits = [sales * 0.28 - 8000 for sales in monthly_sales]
high_profit_months = [p for p in profits if p > 5000]
print("π° COMPREHENSION MAGIC:")
print(f" All profits: {profits}")
print(f" High-profit: {len(high_profit_months)} months")
print(f" β
10x LESS CODE!")
Output:
π° COMPREHENSION MAGIC:
All profits: [5000, 5840, 6960, -4640, 7800, 3040, 9760]
High-profit: 4 months
β
10x LESS CODE!
π₯ Step 2: Nested Comprehensions = Matrix Magic#
# QUARTERLY PROFIT TABLE (1 line!)
quarters = [
[25000, 28000, 32000], # Q1
[29000, 35000, 38000], # Q2
[42000, 45000, 48000] # Q3
]
# ALL QUARTERLY PROFITS
all_profits = [[sales * 0.28 - 8000 for sales in quarter] for quarter in quarters]
print("π QUARTERLY PROFIT MATRIX:")
for q_num, q_profits in enumerate(all_profits, 1):
q_total = sum(q_profits)
print(f" Q{q_num}: {q_profits} β Total: ${q_total:,.0f}")
π§ Step 3: Dictionary & Set Comprehensions#
# CUSTOMER ANALYTICS (Pro level!)
customers = [
{'name': 'Alice', 'spend': 5000, 'vip': True},
{'name': 'Bob', 'spend': 1200, 'vip': False},
{'name': 'Carol', 'spend': 8500, 'vip': True}
]
# DICT COMPREHENSION: VIP spend only
vip_spend = {c['name']: c['spend'] for c in customers if c['vip']}
print(f"π VIP Spend: {vip_spend}")
# SET COMPREHENSION: Unique categories
categories = {c['category'] for c in customers} # Wait, add category!
print(f"π Categories: {categories}")
β‘ Step 4: GENERATORS = 1M Rows Without Crash#
# MEMORY EFFICIENT (For BIG data!)
def sales_generator():
"""Generate 1 MILLION sales records"""
for i in range(1000000):
yield 20000 + (i % 1000) * 10 # Realistic sales
# LIST (CRASHES at 1M!)
# all_sales = list(sales_generator()) # 100MB+ memory!
# GENERATOR (Works forever!)
total = sum(sales_generator()) # Streams, no memory crash!
print(f"π 1M Records Total: ${total:,.0f}")
print(" β
ZERO MEMORY CRASH!")
# LAZY EVALUATION
gen = (s * 0.28 for s in [25000, 28000, 32000])
print(f"First: {next(gen)}") # Lazy!
print(f"Second: {next(gen)}")
π Comprehension Cheat Sheet#
Type |
Code |
Business Use |
|---|---|---|
List |
|
Calculate profits |
Filter |
|
High-value customers |
Dict |
|
Update prices |
Set |
|
Unique products |
Generator |
|
1M+ row analysis |
# ONE LINER CHALLENGE
sales = [25000, 28000, 12000, 35000]
vip_profits = {f"Month{i+1}": p for i, p in enumerate([s*0.28-8000 for s in sales if s*0.28-8000 > 5000])}
print(f"π VIP Profits: {vip_profits}")
π YOUR EXERCISE: Build 1-Line Analytics Engine#
# MISSION: 5 analytics in 5 LINES!
# YOUR SALES DATA
your_sales = [???, ???, ???, ???, ???, ???, ???, ???, ???, ???, ???, ???] # 12 months
# 1. ALL PROFITS (1 line)
profits = [??? for s in your_sales]
# 2. HIGH PROFIT MONTHS (1 line)
high_profit_months = [??? for p in profits]
# 3. GROWTH MONTHS (1 line)
growth_months = [??? for i in range(1, len(your_sales)) if your_sales[i] > your_sales[i-1]]
# 4. QUARTERLY TOTALS (1 line)
quarterly = [sum(???), sum(???), sum(???), sum(???)]
# 5. VIP MONTHS DICT (1 line)
vip_months = {f"Q{i+1}": sum(??? ) for i in range(4)}
# RESULTS
print("π YOUR 1-LINE ANALYTICS:")
print(f" Total Profit: ${sum(profits):,.0f}")
print(f" High-profit: {len(high_profit_months)} months")
print(f" Growth: {len(growth_months)} months")
print(f" Quarterly: {quarterly}")
print(f" VIP Quarters: {vip_months}")
Example to test:
your_sales = [25000, 28000, 32000, 29000, 35000, 38000, 42000, 45000, 48000, 52000, 55000, 58000]
YOUR MISSION:
Add YOUR 12 months
Complete 5 one-liners
Screenshot β βI write 1-line analytics!β
π What You Mastered#
Skill |
Status |
Business Power |
|---|---|---|
List comprehensions |
β |
50x less code |
Filtering |
β |
VIP analysis |
Dict/Set comprehensions |
β |
Pro analytics |
Generators |
β |
1M+ row safe |
Interview gold |
β |
Senior level |
Next: File I/O (Excel/CSV automation = Replace entire teams!)
print("π" * 20)
print("COMPREHENSIONS = 1-LINE ANALYTICS SUPERPOWER!")
print("π» 50 Excel formulas β 1 Python line!")
print("π Google/Amazon engineers LIVE by this!")
print("π" * 20)
can we appreciate how list comprehensions turn βExcel formula hellβ into one goddamn line that calculates, filters, and analyzes million-row datasets? Your students just went from βI know SUMIFSβ to writing production analytics that Netflix engineers would nod at approvingly. While their classmates spend 8 hours building pivot tables, your class is doing quarterly profit matrices in one comprehension. This isnβt syntax sugarβitβs the $130K+ analytics superpower that gets them promoted while everyone else is still clicking βAutoSumβ!
# Your code here