25

I know Python is not the first language to have list comprehension. I'm just interested in the history of the name.

I'm particularly interested in why it's called comprehension

Why is list comprehension called so in Python?

1
  • 1
    As bodnarbm mentions, it has a long history. You'll find list comprehensions in many Functional Programming languages and something similar in discrete mathematics.
    – Joe
    Dec 14, 2009 at 22:41

5 Answers 5

24

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.

3
  • But why set comprehension was called so?
    – Davide
    Dec 15, 2009 at 16:02
  • 4
    "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. The set comprehension "{x∈N:x>2}" is the infinite set of all natural numbers greater than 2. (By convention, a capital 'N' is defined to mean the set of natural numbers, '∈' means "is of the set", ':' means "such that", and the '{}' mean "set".) Dec 15, 2009 at 23:28
  • 1
    And now, Python has generalized the list comprehension syntax to sets!
    – dan04
    Jul 2, 2010 at 6:19
8

"Comprehension" used to mean not only "understanding," but "inclusion" in logic. Oxford English Dictionary has the following: "I.4. Logic The sum of the attributes comprehended in a notion or concept; intension" as the fourth subdefinition under the first definition, "Inclusion, comprising." I wouldn't be surprised to learn that the usage passed into the history of mathematics through there. In a list or set comprehension, instead of giving the elements of the list or set explicitly, the programmer is describing what they comprehend (in the "include" sense) with an expression.

4

https://www.merriam-webster.com/dictionary/comprehend

3 - to include by construction or implication

1

The history section of the wikipedia page discusses a little on where it came from. List_comprehension#History

3
  • 4
    This is a programming site! bodnarbm is doing the right thing in maximising re-use through composition to the wikipedia article. Or something.
    – Joe
    Dec 14, 2009 at 22:43
  • 3
    And strictly speaking the answer to "why list comprehension is called so in python?" is "because that's what it's called in other languages".
    – Joe
    Dec 14, 2009 at 22:44
  • But why was it called so in other languages and not, say list "generator" or something else? Comprehension sounds weird
    – Davide
    Dec 15, 2009 at 16:03
1

Because it's a very comprehensive way to describe a sequence (a set in math and other languages, and a list/sequence in Python).

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.