30

Python programming language, among others, has a construct to create lists which is called a list comprehension.

A simple example:

[(2*n) for n in range(1, 6)]

generates a list of numbers: [2, 4, 6, 8, 10]. For integers between 1 and 5, it multiplies each n by 2 and returns a new list. This can be done for different data structures such as dictionaries and sets.

Wikipedia draws an analogy between set-builder notation and comprehensions: {2n | 1 ≦ n ≦ 5} can be seen as an equivalent of the above list comprehension for integer n. It also implies this notation is also known as "set comprehension".

The main definition of the word ("the ability to understand something") surely doesn't apply here. The Oxford English Dictionary lists a second meaning:

  1. archaic Inclusion.

The answerer here also suggests that it means inclusion in this context:

The name comes from the concept of a set-comprehension

Comprehension is used here to mean complete inclusion or complete description. A set-comprehension is a (usually short) complete description of a set, not an exhaustive (and possibly infinite) enumeration.

I can understand "complete description" but I am having a hard time associating the word "inclusion" with this concept, especially since "inclusion" has a specific meaning in set theory.

Does it really relate to inclusion or does it have a completely separate meaning?

  • 3
    Nitpicking, the notation { 2n | 1 ≦ n ≦ 5} does not correspond to the set theory Axiom Schema of Comprehension (aka. Separation), but rather to the Axiom Schema of Replacement. That would rather be something like { x in Z | exists n such that x = 2n & 1 ≦ n ≦ 5}. – Hagen von Eitzen Aug 21 '17 at 10:59
  • 2
    I submit that "ability to understand something" still applies here, albeit from a different POV: The programming language understands how to resolve the implict function you have written (in this simple case: x->2*x) should be applied to the entire list, and in compact, easily readable notation too! Compare also the answer of @Mitch below. – Axel Aug 21 '17 at 13:27
  • 1
    In the given example, the generated list is length 5, which is shorter that the list comprehension (uses less characters to represent in print). However, consider an example where the generated list is length > 1,000. In that case, the "list compression" may be easier to comprehend (understand) than the generated list. No idea if that contributed to the choice of "list comprehension" or not, but that's how I have remembered it. – Waylan Aug 21 '17 at 19:09
28

It's actually a pretty straightforward use of a secondary definition of "comprehension":

the act or process of comprising

This sense of "comprehension" might be a little clearer if you consider the adjective "comprehensive", meaning "complete".

  • 1
    +1 Interestingly, this is an etymologically circular definition comparing a word derived directly from the Latin with one derived indirectly from the same Latin word via French. That it works as a definition probably makes it a good example of the etymological fallacy. You could roughly read the parts as "taking/holding together before one" or "taking/holding in one's custody/control", which certainly seems to fit. – DeveloperInDevelopment Aug 21 '17 at 11:54
  • 7
    I just realized that I missed an opportunity: I could have made this MUCH more confusing if I'd changed the last sentence to "This sense of 'comprehension' might be more comprehensible..." – MT_Head Aug 21 '17 at 16:20
  • 1
    @DeveloperInDevelopment The etymological fallacy is the belief that the etymology of a word determines its meaning (e.g., the common claim among racists that since "semitic" refers to both Jews and Arabs, an action is not "anti-semitic" unless it discriminates against both). This answer has nothing to do with the etymological fallacy. I don't follow your claim of circular reasoning, either. Circular reasoning would be trying to conclude something about Latin from a premise about Latin (e.g., by going Latin->English->French->Latin); again, that's not happening, here. – David Richerby Aug 22 '17 at 6:06
  • 1
    @NicholasShanks Perhaps you misread my comment. As you know, "Semitic" means related to the Jewish and/or Arabic people, their languages, culture and so on, whereas "Anti-semitic" means discriminatory against Jews. However, many racists try to push the idea that, because of its etymology, the "true" meaning of "anti-semitism" is discrimination against Jews and/or Arabs. They then assert that Israel is anti-semitic, trying to make anti-semitism more acceptable by equating victims with aggressors and generally muddying the aters. – David Richerby Aug 22 '17 at 13:28
  • 1
    @DavidRicherby I meant only that were the etymological fallacy not a fallacy then the definition would border on the tautological. The cycle would be "comprehend->comprise->comprehend". As it happens, the meaning of "comprehend" widened and became more figurative, while "comprise" narrowed and became more literal. Perhaps I should have said "a good example of the fallacious nature of the etymological fallacy". Try reading "comprehending" in place of "comprising" and see whether you think this would still have answered the OP's question as effectively. – DeveloperInDevelopment Aug 22 '17 at 16:19
20

This is a technical use of the word 'comprehension' which has moved on from its original (and currently much more common) meaning of 'understanding'.

It came out of set theory and logic, where 'comprehension' is used figuratively to mean 'implicit construction'.

Then it was borrowed by the LISP programming community (a programming language to implement lambda calculus, used by the set theory/logic community) and then for list programming construct in any programming language.

As a technical word, it means what mathematicians want it to mean.

  • 1
    The historical context really elevates this answer—understanding how the technical meaning evolved significantly clarifies its current meaning. – Jordan Gray Aug 22 '17 at 11:35
  • 1
    This is much better than the accepted answer – Daenyth Aug 22 '17 at 12:26
7

Comprehend comes from the Latin for 'grasp together', as in taking a handful of things; in modern English usage it means to mentally grasp something completely in understanding. The Latin prehendere 'take hold of, seize' also appears in words like apprehend and prehensile, which are more literally about grasping.

So a list comprehension is, conceptually, an operation that 'takes hold' of an entire list at once rather than piece by piece; the elements of the list are 'grasped together', not explicitly iterated over as in a traditional for-loop implementation of a similar operation.

0

The word "comprehension" is used in a technical way in mathematical logic. Perhaps when the Python language specification was written, they had this in mind.

Axiom of Comprehanion
An axiom schema of set theory which states: if P(x) is a property then

{x : P}

is a set. I.e. all the things with some property form a set.

source

-1

Note that the code gives an if-and-only-if condition for inclusion in the collection: you are listing all values 2n for which n is in [1, 2, 3, 4, 5]. The analogy is little loose with lists (which can have repeats and where order matters); the connection is more obvious in the case of set-builder notation where the expression after the vertical bar is always an inclusion predicate.

  • 1
    I'm not sure this really answers the question. – shadowtalker Aug 21 '17 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.