A syntactic construct which provides a concise way to create lists in a style similar to the mathematical set-builder notation. Since several languages support list comprehensions, please use this tag in conjunction with the tag of a programming language.
A list comprehension can be used to construct lists in a very natural, easy way, like a mathematician is used to do. The resulting list definition often tends to be clearer and more concise than lists built using a different approach such as nested for
or while
loops.
List comprehensions tend to consist of an input list or lists, variable bindings, filtering predicates and an output expression, and more or less follow the mathematical set-builder notation in most programming languages which support them.