ArticleslgStudy

science

Comparison of programming languages (list comprehension)

Comparison of programming languages (list comprehension) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Comparison of programming languages (list comprehension) rather than just read about it. In short: 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.

Key takeaways

  • Comparison of programming languages (list comprehension) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Comparison of programming languages (list comprehension) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Comparison of programming languages (list comprehension) from memory before moving on to harder problems.

Reference excerpt

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.

Examples of list comprehension

Boo

List with all the doubles from 0 to 10 (exclusive)

List with the names of the customers based in Rio de Janeiro

C++ C++ can use the std::views namespace, introduced in C++20. These are lazily evaluated.

C#

The previous code is syntactic sugar for the following code written using lambda expressions:

Ceylon

Filtering numbers divisible by 3:

Multiple "generators":

Clojure

An infinite lazy sequence:

A list comprehension using multiple generators:

CoffeeScript

Common Lisp

List comprehensions can be expressed with the loop macro's collect keyword. Conditionals are expressed with if, as follows:

Cobra

List the names of customers:

List the customers with balances:

List the names of customers with balances:

The general forms:

Note that by putting the condition and expression after the variable name and enumerable object, editors and IDEs can provide autocompletion on the members of the variable.

Dart

Elixir

Erlang

F#

Lazily-evaluated sequences:

Or, for floating point values

Lists and arrays:

List comprehensions are the part of a greater family of language constructs called computation expressions.

Haskell

An example of a list comprehension using multiple generators:

Io

By using Range object, Io language can create list as easy as in other languages:

ISLISP

List comprehensions can be expressed with the for special form. Conditionals are expressed with if, as follows:

Julia

Julia supports comprehensions using the syntax:

and multidimensional comprehensions like:

It is also possible to add a condition:

And just changing square brackets to the round one, we get a generator:

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

Multiple generators:

pyth = [ (x,y,z) for x in 1..20 for y in x..20 for z in y..20 where x*x + y*y == z*z ];

Nemerle

Nim Nim has built-in seq, set, table and object comprehensions on the sugar standard library module:

The comprehension is implemented as a macro that is expanded at compile time, you can see the expanded code using the expandMacro compiler option:

The comprehensions can be nested and multi-line:

OCaml

OCaml supports List comprehension through OCaml Batteries.

Perl

Array with all the doubles from 1 to 9 inclusive:

Array with the names of the customers based in Rio de Janeiro (from array of hashes):

Filtering numbers divisible by 3:

PowerShell

which is short-hand notation of:

Python

Python uses the following syntax to express list comprehensions over finite lists:

A generator expression may be used in Python versions >= 2.4 which gives lazy evaluation over its input, and can be used with generators to iterate over 'infinite' input such as the count generator function which returns successive integers:

(Subsequent use of the generator expression will determine when to stop generating values).

R

Racket

An example with multiple generators:

Raku

Scala

Using the for-comprehension:

Scheme

List comprehensions are supported in Scheme through the use of the SRFI-42 library.

An example of a list comprehension using multiple generators:

SETL

Smalltalk

Visual Prolog

S = [ 2*X || X = list::getMember_nd(L), X*X > 3 ]

References

External links Comparison of list comprehensions on rosettacode.org

Worked examples

Example 1 — a first encounter with Comparison of programming languages (list comprehension)

Start with the simplest possible case. Write down what Comparison of programming languages (list comprehension) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Comparison of programming languages (list comprehension) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Comparison of programming languages (list comprehension) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Comparison of programming languages (list comprehension)

In research
Comparison of programming languages (list comprehension) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Comparison of programming languages (list comprehension) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Comparison of programming languages (list comprehension) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming language comparisons, so understanding it makes those chapters shorter.
In everyday life
Look for Comparison of programming languages (list comprehension) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Comparison of programming languages (list comprehension)” →

How to study Comparison of programming languages (list comprehension) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Comparison of programming languages (list comprehension) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Comparison of programming languages (list comprehension) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Comparison of programming languages (list comprehension) in simple terms?

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.

Why does Comparison of programming languages (list comprehension) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Comparison of programming languages (list comprehension)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Comparison of programming languages (list comprehension).

Tags

  • Programming language comparisons

Keep exploring