σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademySkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
Skills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.11·~30 minBeginner

List comprehensions

Skill positionModule 1 of 11 · Topic 11 of 33
1.11 List comprehensions
—Lab score— / 100Total XP—Streak—

From a loop to one expression

A very common loop starts with an empty list and appends one result per item. A list comprehension says the same thing in one expression, inside square brackets: [what_to_build for item in collection]. Read it from left to right as "build this, for each item in that". It is shorter, and it shows at a glance that the whole thing is a new list.

prices = [184.0, 185.0, 183.0]

# The loop way
notionals = []
for price in prices:
    notionals.append(price * 100)
print(notionals)  # [18400.0, 18500.0, 18300.0]

# The comprehension way: the same list in one line
notionals = [price * 100 for price in prices]
print(notionals)  # [18400.0, 18500.0, 18300.0]

Keeping only some items: if

Add if condition at the end to keep only the items that pass a test. This is how you filter: positive returns, orders above a size, symbols that match. Items that fail the test are simply left out, and the order of the rest is kept.

# Keep only the items that pass a test
returns = [0.01, -0.02, 0.005, -0.001, 0.03]
gains = [r for r in returns if r > 0]
print(gains)  # [0.01, 0.005, 0.03]

Positions and pairs

The for part works like a normal loop: it can walk through range(...) to use positions, and it can name both halves of a pair (for symbol, price in quotes). Use positions when each result depends on a neighbour, such as a return from the previous price; use pairs when each item holds several fields.

# Simple returns from a price series, using positions
prices = [100.0, 102.0, 101.0]
returns = [prices[i] / prices[i - 1] - 1 for i in range(1, len(prices))]
print([round(r, 4) for r in returns])  # [0.02, -0.0098]

# Pairs can be unpacked, and the result can be transformed
quotes = [("aapl", 184.5), ("msft", 410.2), ("goog", 171.0)]
above = [symbol.upper() for symbol, price in quotes if price > 180]
print(above)  # ['AAPL', 'MSFT']

if/else belongs in front

A trailing if removes items. If you want to change each item depending on a condition, use the conditional expression a if condition else b as the first part. Mixing the two up is a common mistake: if alone goes after the for, if ... else goes before it.

# if/else changes each item; a plain if (at the end) drops items
returns = [0.01, -0.02, 0.005, -0.001, 0.03]
signals = ["long" if r > 0 else "flat" for r in returns]
print(signals)  # ['long', 'flat', 'long', 'flat', 'long']
Build lists, do not perform actionsA comprehension is for producing a list from another one. If you only want to print, send orders or change something, use a normal for loop. If a comprehension gets long or nested, split it into a loop or into a helper function: readability beats cleverness.
  • [expression for item in items]: one result per item.
  • ... if condition at the end: keep only some items.
  • a if condition else b at the front: change each item.

Next up: lists, the collection these loops and comprehensions have been working on: indexing, slicing, adding, removing and sorting.

Lab · what's graded

  • Value every trade in one expression with notionals25 pts
  • Turn prices into returns with simple_returns25 pts
  • Keep only the gains with positive_returns25 pts
  • Filter and clean symbols together with symbols_above25 pts
Explorer · lab-11-1
▾ lab-11-1
…
bash · in-browser
learner@quantcraft:~/lab-11-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-11-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub