{
  "id": 14040462,
  "title": "Nested Loops in a Python List Comprehension",
  "url": "https://urgent.news/2026/10/12/nested-loops-in-a-python-list-comprehension",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-12T20:40:49.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/peakblick/nested-loops-in-a-python-list-comprehension-1614"
  },
  "original_language": "en",
  "account": "In Python, a list comprehension can contain multiple \"for\" clauses, which allows the creation of nested loop-like behavior. However, the order of these clauses can be counterintuitive for newcomers. The crucial rule to remember is that the clauses should be written in the same order as nested loops, with the outer loop first, followed by the inner loop.\n\nConsider the following example:\npairs = [(x, y) for x in range(3) for y in range(3)]\nThis line of code is equivalent to the following nested loop structure:\npairs = []\nfor x in range(3):\nfor y in range(3):\npairs.append((x, y))\n\nThe mental model for understanding this concept is to copy the loops from top to bottom and place the expression (in this case, (x, y)) at the front. The inner loop runs fully for each iteration of the outer loop, just like in traditional nested loops.\n\nA common mistake is to write the clauses in reverse order, such as:\npairs = [ (x, y) for y in range(3) for x in range(3) ]\nThis will result in a NameError, as the inner variable (y) is used before it is defined by the outer loop.\n\nWhen using list comprehensions with nested loops, it's essential to reorder the clauses according to their loop order to avoid such errors. This rule holds true when flattening a list of lists, as demonstrated in the following example:\nmatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nflat = [n for row in matrix for n in row]\nThis comprehensions flattens the matrix into a single list: [1, 2, 3, 4, 5, 6, 7, 8, 9].\n\nIt's important to distinguish between nested comprehensions and multiple \"for\" clauses. Nested comprehensions generate a list of lists, while multiple \"for\" clauses create a flat list. For example:\ngrid = [[col for col in range(3)] for row in range(2)]\nThis creates a 2x3 grid: [[0, 1, 2], [0, 1, 2]]\n\nIn summary, when working with nested loops in Python list comprehensions, remember that the clauses should be ordered from outer to inner, and this order is crucial for avoiding NameErrors and ensuring the desired behavior. When the comprehensions become too complex or unreadable, it's often better to opt for traditional nested loops for improved readability and maintainability.",
  "summary": "You can put more than one for in a list comprehension to get the effect of nested loops — but the order of those for clauses trips almost everyone up the first time. Here's how it actually works, with the one rule that makes it click. The basic idea: same order as nested loops A comprehension with two for clauses behaves exactly like two nested for loops, and — this is the whole trick — the…",
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}