You are on page 1of 13

List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.

htm

List comprehension
From Wikipedia, the free encyclopedia

A list comprehension is a syntactic construct available in some programming


languages for creating a list based on existing lists. It follows the form of the
mathematical set-builder notation (set comprehension) as distinct from the use of
map and filter functions.

Contents
1 Overview
2 History
3 Examples in different programming languages
3.1 B-Prolog
3.2 Clojure
3.3 Common Lisp
3.4 Erlang
3.5 F#
3.6 Falcon
3.7 Haskell
3.8 JavaFX
3.9 Javascript 1.8
3.10 Mythryl
3.11 OCaml
3.12 Pure
3.13 Python
3.14 Scala
3.15 Visual Prolog
4 Similar constructs
4.1 Monad comprehension
4.2 Set comprehension
4.3 Dict comprehension
4.4 Parallel list comprehension
4.5 XQuery and XPath
4.6 LINQ in C#
5 See also
6 Notes and references
6.1 Haskell

1 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

6.2 OCaml
6.3 Python
6.4 Common Lisp
6.5 Clojure
6.6 Axiom
7 External links

Overview
Consider the following example in set builder notation.

This can be read, " S is the set of all


2 times x where x is an item in the set of
natural numbers, for which x squared is greater than 3 ."

In this annotated version of the example:

x is the variable representing members of an input set.


represents the input set, which in this example is the set of natural
numbers
x2 > 3 is a predicate function acting as a filter on members of the input
set.
is an output function producing members of the new set from members
of the input set that satisfy the predicate function.
{} brackets contain the expression
, the vertical bar and the comma are separators.
A list comprehension has the same syntactic components to represent generation
of a list in order from an input list or iterator:

A variable representing members of an input list.


An input list (or iterator).
An optional predicate expression.
And an output expression producing members of the output list from
members of the input iterable that satisfy the predicate.

2 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

The order of generation of members of the output list is based on the order of
items in the input.

In Haskell's list comprehension syntax, this set-builder construct would be


written similarly, as:
s = [ 2*x | x <- [0..], x^2 > 3 ]

Here, the list [0..] represents , x^2>3 represents the predicate, and 2*x
represents the output expression.

List comprehensions give results in a defined order (unlike the members of sets);
and list comprehensions may generate the members of a list in order, rather than
produce the entirety of the list thus allowing, for example, the previous Haskell
definition of the members of an infinite list.

History
The SETL programming language (later 1960s) had a set formation construct,
and the computer algebra system AXIOM (1973) has a similar construct that
processes streams, but the first use of the term "comprehension" for such
constructs was in Rod Burstall and John Darlington's description of their
functional programming language NPL from 1977.

Burstall and Darlington's work with NPL influenced many functional


programming languages during the 1980s but not all included list
comprehensions. An exception was the influential pure lazy functional
programming language Miranda which was released in 1985. The subsequently
developed standard pure lazy functional language, Haskell, includes many of
Miranda's features including list comprehensions. The scripting language Python
was heavily influenced by the pure lazy school of functional programming and
adopted list comprehensions. Pure functional programming remains a niche
while Python achieved wider adoption which introduced list comprehensions to a
wider audience.
[1]
Comprehensions were proposed as a query notation for databases and were
[2]
implemented in the Kleisli database query language.

Examples in different programming languages


Main article: Comparison of programming languages (list comprehension)

The following provides a few examples of specific syntax used in programming


languages.

3 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

Although the original example denotes an infinite list, few languages can express
that, so in some of those cases we show how to take a subset of {0,1,...,100}
rather than a subset of .

B-Prolog

Further information: BProlog


(L @= [Y : X in 1..100, [Y], (X*X>3, Y is 2*X)]

A list of the form [T : E1 in D1, ..., En in Dn, LocalVars, Goal] is


interpreted as a list comprehension in calls to @=/2 and constraints. A list
comprehension is translated into a foreach construct with an accumulator.

Clojure

Further information: Clojure

Clojure generates infinite lazy sequences (similar to Haskell's lazy lists or


Python's generators). Use take to get the first N results from the infinite
sequence.
(take 20 (for [x (iterate inc 0) :when (> (* x x) 3)] (* 2 x)))

Common Lisp

Further information: Common Lisp

Although there is no standard list comprehension syntax in the language, a


syntax can be created.

Using the loop macro:


(loop for x from 1 to 20 when (> (* x x) 3) collect (* 2 x))

A list comprehension macro can be defined.


(defmacro comprehend (variable input predicate output)
;apply output function
`(mapcar (lambda (,variable) ,output)
;to each input that passes the predicate
(remove-if-not (lambda (,variable) ,predicate) ,input)))

(setq 1-20 (loop for x from 1 to 20 collect x))

;It can be used like any other construct


(comprehend x 1-20 (> (* x x) 3) (* 2 x))

Additional parameters can be added to this macro but ignored, if the

4 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

programmer would rather type


(comprehend x from 1-20 where (> (* x x) 3) select (* 2 x))

or something similar.

An infinite lazy sequence can be created in a variety of ways, such as the CLOS
object system or a yield macro.

Erlang

Further information: Erlang

The same example in Erlang:


S = [2*X || X <- lists:seq(0,100), X*X > 3].

F#

Further information: F#

The F# generator comprehension has the list comprehension syntax elements.


Generator comprehensions can be used to generate Lists, Sequences (like lists
but evaluated on-demand) and Arrays (not discussed here).

Generators are of the form [for x in collection do ... yield expr] for lists
and seq {for x in collection do ... yield expr} for sequences. For
example:
> seq { for x in 0..100 do
if x*x > 3 then yield 2*x } ;;
val it : seq<int> = seq [4; 6; 8; 10; ...]

Falcon

Further information: Falcon

The "comp" generic method family provides wide support for comprehension. For
example, the "mfcomp" method can be applied to an array:

s = [].mfcomp( { i => if i*i > 3: return 2*i; return oob(1)}, [1:101] )

Falcon can also use functional generators to provide input lists. For example, the
following code uses a continuation to create a set of pairs.

5 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

gen = Continuation( function( max, c )


i = 0
while i < max: c(++i)
return oob(0)
end )
data = [10,11,12]
s = Set().mfcomp( {x, y => x+y}, .[gen 3], data )

Method "comp" was introduced in version 0.9.6, and methods "mcomp" and
"mfcomp" in version 0.9.6.2.

Haskell

Further information: Haskell

Please refer to the main example in the overview.

JavaFX

Further information: JavaFX

Using the for statement and a boolean expression:


var s = for (i in [1..100][n | n*n > 3]) { 2*i }

Javascript 1.8

Further information: Javascript


Number.prototype.__iterator__=function(){for (let i=0; i<this; i++) yield i}

var s = [2*i for (i in 100) if (i*i>3)]

Mythryl

Further information: Mythryl

s = [ 2*i for i in 1..100 where i*i > 3 ];

OCaml

Further information: OCaml

OCaml Batteries Included has uniform comprehension syntax for lists, arrays,
enumerations (like streams), lazy lists (like lists but evaluated on-demand), sets,
hashtables, etc.

6 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

Comprehension are of the form [? expression | x <- enumeration ;


condition; condition ; ... ?].

For instance,
# [? 2 * x | x <- 0 -- max_int ; x * x > 3 ?];;
- : int Enum.t = <abstr>

or, to compute a list,


# [? List: 2 * x | x <- 0 -- 5 ; x * x > 3 ?];;
- : int list = [4; 6; 8; 10]

or, to compute a set,


# [? PSet: 2 * x | x <- 0 -- 5 ; x * x > 3 ?];;
- : int PSet.t = <abstr>

etc.

Pure

Further information: Pure

The same example in Pure:

s = [2*n | n = (1..100); n*n > 3];

Python

Further information: Python syntax and semantics: Generators

The Python programming language has a corresponding syntax for expressing


list comprehensions. The near-equivalent in Python to the example above is as
follows:
S = [2*x for x in range(101) if x**2 > 3]

List comprehensions were introduced in Python version 2.0.[3]

A generator expression may be used in Python versions 2.4 and above to


achieve functional equivalence with S using a generator to iterate an infinite list:
from itertools import count
S = (2*x for x in count() if x**2 > 3)

Python list comprehension range from simple:

7 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

M = [[1, 2, 3], [4,5,6], [7,8,9]] # This is an example of a nested list, (a list inside a list).
col2 = [row[1] for row in M] # The logic goes like this: since it starts from 0, not 1, row[1]
# selects the second part of each list. (2, 5, and 8.)

to a more complicated expression:


M = [[1, 2, 3], [4,5,6], [7,8,9]]
list = [row[1] for row in M if row[1] % 2 == 0] # The second number of each nested list
# IF the modulus division, (the remainder of a division), is 0.
# This ensures that it only gets even numbers, and not odd numbers.
# (Any odd number divided by 2 has a remainder, but any even number has no remainder.)
# The output of this would be [2, 8]. The only difference between this one and the last
# is that it omits 5, an odd number. If statements can be quite powerful in list comprehension.

List comprehension in Python speeds up the process of doing certain tasks, like
getting certain numbers out of another list. (Or any other data type that can be
looped through).

Scala

Further information: Scala

Using the for-comprehension:


val s = for (x <- Stream.from(0); if x*x > 3) yield 2*x

Visual Prolog

Further information: Visual Prolog


S = [ 2*X || X = std::fromTo(1, 100), X^2 > 3 ]

Similar constructs
Monad comprehension

In Haskell, a monad comprehension is a generalization of the list comprehension


to other monads in functional programming.

Set comprehension

Version 3 of the Python language introduces syntax for set comprehensions.


Similar in form to list comprehensions, set comprehensions generate Python sets
instead of lists.

8 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

>>> s = {v for v in 'ABCDABCD' if v not in 'CB'}


>>> print(s)
{'A', 'D'}
>>> type(s)
<class 'set'>
>>>

Dict comprehension

Version 3 of the Python language introduces separate syntax for dictionary


comprehensions, similar in form to list comprehensions.
>>> d = {k:v for k,v in enumerate('ABCD') if v not in 'CB'}
>>> print(d)
{0: 'A', 3: 'D'}
>>> type(d)
<class 'dict'>
>>>

Parallel list comprehension

The Glasgow Haskell Compiler has an extension called parallel list


comprehension (also known as zip-comprehension) that permits multiple
independent branches of qualifiers within the list comprehension syntax.
Whereas qualifiers separated by commas are dependent, qualifier branches
separated by pipes are evaluated in parallel (this does not refer to any form of
multithreadedness: it merely means that the branches are zipped).

XQuery and XPath

Like the original NPL use, these are fundamentally database access languages.

This makes the comprehension concept more important, because it is


computationally infeasible to retrieve the entire list and operate on it (the initial
'entire list' may be an entire XML database).

In XPath, the expression:

/library/book//paragraph[@style='first-in-chapter']

is conceptually evaluated as a series of "steps" where each step produces a list


and the next step applies a filter function to each element in the previous step's
[4]
output.

In XQuery, full XPath is available, but FLWOR statements are also used, which is
[5]
a more powerful comprehension construct.

9 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

for $b in //book
where $b[@pages < 400]
order by $b//title
return
<shortBook>
<title>{$b//title}</title>
<firstPara>{($book//paragraph)[1]}</firstPara>
</shortBook>

Here the XPath //book is evaluated to create a sequence (aka list); the where
clause is a functional "filter", the order by sorts the result, and the
<shortBook>...</shortBook> XML snippet is actually an anonymous function
that builds/transforms XML for each element in the sequence using the 'map'
approach found in other functional languages.

So, in another functional language the above FLWOR statement may be


implemented like this:

map(
newXML(shortBook, newXML(title, $1.title), newXML(firstPara, $1...))
filter(
lt($1.pages, 400),
xpath(//book)
)
)

LINQ in C#

C# 3.0 has a group of related features called LINQ, which defines a set of query
operators built to manipulate list-like structures (object enumerations, SQL
datasets, …) which can be used in ways similar to list comprehension:
var s = from x in Enumerable.Range(0, 100) where x*x > 3 select x*2;

See also
The SELECT statement together with its FROM and WHERE clauses in SQL
Programming language
Mathematical notation
Monads in functional programming for monads and monadic notation in
general
For other programming language constructs used to process sequences:
Generator (computer science)
Map (higher-order function)
For other programming language constructs copied from the mathematical
notation:
Guard (computing)

10 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

Pattern matching
Operator (programming)

Notes and references


1. ^ Comprehensions, a query notation for DBPLs (http://portal.acm.org
/citation.cfm?coll=GUIDE&dl=GUIDE&id=135271)
2. ^ The functional guts of the Kleisli query system (http://portal.acm.org
/citation.cfm?id=351241&dl=ACM&coll=portal)
3. ^ Warsaw, Barry (2 October 2008). "PEP 202 – List Comprehensions"
(http://www.python.org/dev/peps/pep-0202/) . python.org. http://www.python.org
/dev/peps/pep-0202/.
4. ^ "2.1 Location Steps" (http://www.w3.org/TR/xpath#section-Location-Steps) . XML
Path Language (XPath). W3C. 16 November 1999. http://www.w3.org
/TR/xpath#section-Location-Steps.
5. ^ "XQuery FLWOR Expressions" (http://www.w3schools.com/XQuery
/xquery_flwor.asp) . W3Schools. http://www.w3schools.com/XQuery/xquery_flwor.asp.

List Comprehension (http://ftp.sunet.se/foldoc


/foldoc.cgi?list+comprehension) in The Free On-line Dictionary of
Computing, Editor Denis Howe.
Trinder, Phil (1992). "Comprehensions, a query notation for DBPLs"
(http://portal.acm.org/citation.cfm?coll=GUIDE&dl=GUIDE&id=135271) .
Proceedings of the third international workshop on Database programming
languages: bulk types & persistent data, Nafplion, Greece
(http://portal.acm.org/citation.cfm?coll=GUIDE&dl=GUIDE&id=135271) .
pp. 55–68. http://portal.acm.org/citation.cfm?coll=GUIDE&dl=GUIDE&
id=135271.
Wadler, Philip (1990). "Comprehending Monads" (http://citeseer.ist.psu.edu
/wadler92comprehending.html) . Proceedings of the 1990 ACM Conference
on LISP and Functional Programming, Nice (http://citeseer.ist.psu.edu
/wadler92comprehending.html) . http://citeseer.ist.psu.edu
/wadler92comprehending.html.
Wong, Limsoon (2000). "The Functional Guts of the Kleisli Query System"
(http://portal.acm.org/citation.cfm?id=351241&dl=ACM&coll=portal) .
Proceedings of the fifth ACM SIGPLAN international conference on
Functional programming (http://portal.acm.org/citation.cfm?id=351241&
dl=ACM&coll=portal) . International Conference on Functional
Programming. pp. 1–10. http://portal.acm.org/citation.cfm?id=351241&
dl=ACM&coll=portal.

Haskell

The Haskell 98 Report, chapter 3.11 List Comprehensions (http://haskell.org

11 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

/onlinereport/exps.html#list-comprehensions) .
The Glorious Glasgow Haskell Compilation System User's Guide, chapter
7.3.4 Parallel List Comprehensions (http://www.haskell.org/ghc/docs/latest
/html/users_guide/syntax-extns.html#parallel-list-comprehensions) .
The Hugs 98 User's Guide, chapter 5.1.2 Parallel list comprehensions (a.k.a.
zip-comprehensions) (http://cvs.haskell.org/Hugs/pages/users_guide/hugs-
ghc.html#ZIP-COMPREHENSION) .

OCaml

OCaml Batteries Included (http://batteries.forge.ocamlcore.org/)


Language extensions introduced in OCaml Batteries Included
(http://batteries.forge.ocamlcore.org/doc.preview:batteries-alpha3
/html/extensions.html)

Python

Python Reference Manual, chapter 5.2.4 List displays


(http://www.python.org/doc/2.4.1/ref/lists.html) .
Python Enhancement Proposal PEP 202: List Comprehensions
(http://www.python.org/peps/pep-0202.html) .
Python Reference Manual, chapter 5.2.5 Generator expressions
(http://www.python.org/doc/2.4.1/ref/genexpr.html) .
Python Enhancement Proposal PEP 289: Generator Expressions
(http://python.org/peps/pep-0289.html) .

Common Lisp

Implementation of a Lisp comprehension macro (http://rali.iro.umontreal.ca


/Publications/urls/LapalmeLispComp.pdf) by Guy Lapalme

Clojure

Clojure API documentation - for macro (http://clojure.org/api#for)

Axiom

Axiom stream examples (http://page.axiom-developer.org/zope/mathaction


/Streams)

External links
SQL-like set operations with list comprehension one-liners in the Python

12 of 13 Friday 15 July 2005 01:15 AM


List comprehension - Wikipedia, the free encyclopedia file:///media/Memory%20card/List_comprehension.htm

Cookbook (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe
/159974)
Discussion on list comprehensions in Scheme and related constructs
(http://lambda-the-ultimate.org/classic/message11326.html)
List Comprehensions across languages (http://langexplr.blogspot.com
/2007/02/list-comprehensions-across-languages_18.html)
Retrieved from "http://en.wikipedia.org/wiki/List_comprehension"
Categories: Programming constructs | Articles with example code | Articles with
example Haskell code | Articles with example Python code

This page was last modified on 22 June 2010 at 17:42.


Text is available under the Creative Commons Attribution-ShareAlike
License; additional terms may apply. See Terms of Use for details.
Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a
non-profit organization.

Privacy policy
About Wikipedia
Disclaimers

13 of 13 Friday 15 July 2005 01:15 AM

You might also like