Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

TaskExcelComprehensionLines Saved
Filter profits10 formulas[p for p in profits if p > 5000]50x
Calculate margins20 formulas[s*0.28 for s in sales]100x
VIP customers5 filters[c for c in customers if c['vip']]Infinite
Growth monthsPivot table[s for s in sales if s > sales[i-1]]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

TypeCodeBusiness Use
List[x*2 for x in data]Calculate profits
Filter[x for x in data if x > 100]High-value customers
Dict{k: v*2 for k,v in dict.items()}Update prices
Set{x for x in data if condition}Unique products
Generator(x*2 for x in data)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:

  1. Add YOUR 12 months

  2. Complete 5 one-liners

  3. Screenshot“I write 1-line analytics!”


🎉 What You Mastered

SkillStatusBusiness Power
List comprehensions50x less code
FilteringVIP analysis
Dict/Set comprehensionsPro analytics
Generators1M+ row safe
Interview goldSenior 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

Exercises

Exercise 1

Write filter_even_squares(nums) that returns squares of even numbers using a list comprehension.


Exercise 2

Create sum_squares_gen(nums) that returns a generator expression for squares and use it to compute the sum.


Exercise 3

Implement index_map(items) returning a dict mapping item->index using a dict comprehension.