promptdojo_

Comprehensions and the wrong-layer bug — step 8 of 8

Going deeper: your AI assistant is the syntax reference now

This chapter gave you three shapes — the list-and-dict tree, the loop that walks it, the comprehension that collapses the walk to one line. Real codebases will keep throwing constructs at you that this course hasn't drilled yet: a comprehension with two for clauses, a slice like items[1:-1], an unpacking like a, *rest = row.

You don't need a textbook for those. You have an AI assistant open in the next tab. What you need is a prompt pattern that turns any unfamiliar line into an explanation you can check.

The prompt pattern: explain it three ways

When a line of Python resists your reading, paste it into your assistant with this:

Explain this Python line three ways:
1. As one plain-English sentence.
2. Rewritten as a longer, step-by-step version with no shortcuts.
3. As a tiny runnable example using different data, showing the
   exact expected output.

Line: [paste the construct here]

Why three ways? Each one catches a different kind of confusion:

  • The sentence tells you the intent — what the line is for.
  • The long version shows you the mechanics — a comprehension expands back into the loop-and-append you read in this lesson.
  • The runnable example with different data is the one that keeps the explanation honest. It's a testable claim, not a vibe.

Run it. Don't trust it.

Number 3 is the whole game. An AI explanation of syntax is usually right — and when it's wrong, it's wrong exactly where this chapter warned you: the wrong layer, the silently-dropped item, the filter that replaced instead of removed.

So the habit is: take the tiny example it gave you, run it, and compare the real output to the output it claimed. If they match, you just verified the explanation for free. If they don't, you found the gap — and now you have a very pointed follow-up question to ask.

The editor on the right has a paste-worthy specimen: a double-for comprehension digging emails out of a nested org. Run it first, note the output, then try the three-ways prompt on it and see whether your assistant's step-by-step version matches what actually printed.

The neutral referee

When your assistant's answer and your intuition disagree, there's a tiebreaker that has no stake in the argument: the official Python documentation at docs.python.org. The tutorial's Data Structures chapter covers lists, dicts, and comprehensions; the Library Reference covers every built-in. It's dry, but it's the ground truth every AI was trained on.

The pecking order, permanently:

  1. The interpreter — what the code actually does when you run it.
  2. docs.python.org — what the language promises.
  3. Your assistant's explanation — a fast, usually-right summary of the first two.

Use the fast one daily. Check it against the other two whenever money, data, or your name is attached to the output.

Run the editor. One active member, one email in the list — and one construct you can now interrogate anywhere you meet it.