W WolfCode · CSC 141

List Comprehension

列表推导式

≈ 20 min · python-lists python-loops · Open in WolfCode

The map + filter pattern from Think Python §10.7, compressed into one line.

The shape

[ expression  for  var  in  iterable  if  condition ]
Squares of every even number in 0..n

Pure filter — leave the expression as the variable

Python · runnable

Equivalent loop version

Python · runnable