Python by Structure: 리스트 컴프리헨션과 '단일 액션'
Source: Dev.to
Timothy’s “Assembly Line” Code
squares = []
for n in range(10):
if n % 2 == 0:
squares.append(n**2)
The Problem: The Three‑Level Deep Append
Margaret pasted Timothy’s code into the Python Structure Viewer.
Python Structure Viewer output
=== TREE VIEW ===
squares = []
For n in range(10)
If n % 2 == 0
└── squares.append(n ** 2)
=== ENGLISH VIEW ===
Set squares to [].
For each n in range(10):
If n % 2 == 0:
Evaluate squares.append(n ** 2).
Margaret noted that the actual work (squares.append) is three levels deep, making the logic scattered and harder to follow.
The Solution: Structural Transformation
“Python allows you to collapse that entire process into a single structural unit,” Margaret explained. She refactored the code into a list comprehension.
The Structured Python Snippet
squares = [n**2 for n in range(10) if n % 2 == 0]
Running it through the Viewer produces:
Python Structure Viewer output
=== TREE VIEW ===
squares =
Comprehension: n ** 2
For n in range(10)
If n % 2 == 0
=== ENGLISH VIEW ===
Set squares to:
List comprehension: n ** 2
For each n in range(10)
If n % 2 == 0
Timothy observed that the Viewer now describes the assignment as the result of a single operation rather than a series of mutable steps.
The “Factory” Metaphor
Timothy compared the two versions:
“The first version is like building a car by hand on a floor—frame first, then engine, then wheels. The comprehension is like a factory mold. I define the mold, and the car comes out finished in one piece.”
Margaret agreed, noting that the comprehension reduces cognitive load by presenting the final result directly, without requiring the reader to track a mutating list.
Analyze Python Structure Yourself
Download the Python Structure Viewer — a free tool that shows code structure in tree and plain‑English views. Works offline, no installation required.