[Python-Dev] RE: list comprehensions (was parsers and compilers for 2.0) (original) (raw)

Greg Ewing greg@cosc.canterbury.ac.nz
Mon, 14 Aug 2000 16:17:30 +1200 (NZST)


Two reasons why list comprehensions fit better in Python than the equivalent map/filter/lambda constructs:

  1. Scoping. The expressions in the LC have direct access to the enclosing scope, which is not true of lambdas in Python.

  2. Efficiency. An LC with if-clauses which weed out many potential list elements can be much more efficient than the equivalent filter operation, which must build the whole list first and then remove unwanted items.

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+