Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 6 · FAANG-2026 deep divesWalrus in comprehensions — scope leak gotchapredict112 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. \`data = ["py", "hello", "lambda", "ok"]\`. Two comprehensions filter by length, using walrus to bind \`n\` / \`m\`. Both finish — predict what \`n\` and \`m\` hold AFTER the comprehensions finish. Hint: walrus binds for every iteration, including filtered-out ones. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

data = ["py", "hello", "lambda", "ok"]

matches = [(s, n) for s in data if (n := len(s)) > 3]
print(matches)
print(f"n_after:{n}")

gen = ((s, m) for s in data if (m := len(s)) > 4)
print(list(gen))
print(f"m_after:{m}")

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…