Skip to content

Length ​

The number of elements in the collection.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageLength!
Length(collection)the number of elements in the collection.
SageMathenumeratio_length(collection)Oscarlength(collection)
Length(expr)the number of top-level operands of any expression, not just a collection.
SageMathenumeratio_length(expr)Oscarlength(expr)
Length(finset)the number of members of a Finset, its cardinality.
SageMathenumeratio_length(finset)Oscarlength(finset)
Typed signatures
TypePackageApplies
(any) -> infinity | integercombinatoricsreplaces compute-enginealways
(any) -> infinity | integercompute-enginealways

Domain: Collections

Details
  • Works on any collection head, not just ListList — e.g. SetSet.
  • Additive over concatenation: Length(Join(A,B))=Length(A)+Length(B)Length(Join(A, B)) = Length(A) + Length(B). See Join.
  • An atom has no parts, so its length is 0 — it isn't a type error.
  • Any other expression's length is its number of top-level operands, e.g. the number of terms in a sum.
  • See Count to count occurrences of a specific value instead of every element.
  • A Finset is read as the subset it is, so its length is its number of members. compute-engine has no Cardinality head, so this is its cardinality.

Examples

1 more kept as data, checked against the oracles
Basic #
live ↗
InLength([1,2,3,4])\mathrm{Length}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out44
live ↗
InLength([1,2,3,4,5])\mathrm{Length}(\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack)Out55

Works on any collection, not just ListList

live ↗
InLength({1,2,3})\mathrm{Length}(\lbrace1, 2, 3\rbrace)Out33
live ↗
InLength([a,b,c,d])\mathrm{Length}(\bigl\lbrack a, b, c, d\bigr\rbrack)Out44

An empty association has no entries

live ↗
InLength(Association())\mathrm{Length}(\mathrm{Association}())Out00
Scope #

An atom has no parts, so its length is 0

live ↗
InLength(5)\mathrm{Length}(5)Out00

The empty list has length 0

live ↗
InLength([])\mathrm{Length}(\bigl\lbrack\bigr\rbrack)Out00

Of a matrix, the number of rows

live ↗
InLength([[1,2],[3,4],[5,6]])\mathrm{Length}(\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 4\bigr\rbrack, \bigl\lbrack5, 6\bigr\rbrack\bigr\rbrack)Out33

A SetSet holds each element once, so a repeated element counts once

live ↗
InLength({1,1,2})\mathrm{Length}(\lbrace1, 1, 2\rbrace)Out22

Any expression: the number of terms in a sum

live ↗
InLength(a+b+c+d)\mathrm{Length}(a+b+c+d)Out44

The number of arguments of a call, counting only the top level

live ↗
InLength(f(g(x,y),z))\mathrm{Length}(f(g(x, y), z))Out22

A symbol such as π\pi is an atom, of length 0

live ↗
InLength(π)\mathrm{Length}(π)Out00

A lazy Range is counted without being materialised

live ↗
InLength(1..20)\mathrm{Length}(1..20)Out2020

A symbol is atomic and has no parts

live ↗
InLength(x)\mathrm{Length}(x)Out00

Integers are atoms, so no parts

live ↗
InLength(123 456)\mathrm{Length}(123\,456)Out00

A rational literal counts as an atom here

live ↗
InLength(110)\mathrm{Length}(\frac{1}{10})Out00

A complex number literal is atomic

live ↗
InLength(3+i)\mathrm{Length}(3+i)Out00

A string is atomic, not a list of characters

live ↗
InLength(string)\mathrm{Length}(\text{string})Out00

A Finset has its cardinality: the subset {1,3,4}\{1,3,4\} of {1,…,6}\{1,\dots,6\} has 3 members

live ↗
InLength(Finset⁡(Tuple⁡(6,[1,3,4])))\mathrm{Length}(\operatorname{Finset}(\operatorname{Tuple}(6,\bigl\lbrack1, 3, 4\bigr\rbrack)))Out33

The empty Finset has no members

live ↗
InLength(Finset⁡((4,[])))\mathrm{Length}(\operatorname{Finset}((4,\bigl\lbrack\bigr\rbrack)))Out00
Applications #

For a sum, the number of terms

live ↗
InLength(x9+x2+x+1)\mathrm{Length}(x^9+x^2+x+1)Out44

1000!1000! has 25682568 decimal digits

live ↗
InLength(IntegerDigits(1 000!))\mathrm{Length}(\mathrm{IntegerDigits}(1\,000!))Out2 5682\,568

Row lengths of a ragged list

live ↗
InMap(Length,[[a,b],[a,b,c],[x]])\mathrm{Map}(\mathrm{Length}, \bigl\lbrack\bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack x\bigr\rbrack\bigr\rbrack)Out[2,3,1]\bigl\lbrack2, 3, 1\bigr\rbrack
live ↗
InLength([[a,b,c],[d,k,f]])\mathrm{Length}(\bigl\lbrack\bigl\lbrack a, b, c\bigr\rbrack, \bigl\lbrack d, k, f\bigr\rbrack\bigr\rbrack)Out22
Properties #

Length(Join(A,B))=Length(A)+Length(B)Length(Join(A, B)) = Length(A) + Length(B). See Join

live ↗
InLength(Join([1,2,3],[4,5]))=Length([1,2,3])+Length([4,5])\mathrm{Length}(\mathrm{Join}(\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack4, 5\bigr\rbrack))=\mathrm{Length}(\bigl\lbrack1, 2, 3\bigr\rbrack)+\mathrm{Length}(\bigl\lbrack4, 5\bigr\rbrack)OutTrue\mathrm{True}
Possible issues #

1679 has 1025 squarefree numbers below it, one past compute-engine's own iteration budget (1024) for counting a lazy collection element by element -- this declines rather than silently answer a wrong, short count; see IsSquareFree

live ↗
InLength(Filter(1..1 679,IsSquareFree))\mathrm{Length}(\mathrm{Filter}(1..1\,679, \mathrm{IsSquareFree}))OutLength(Filter(1..1 679,IsSquareFree))\mathrm{Length}(\mathrm{Filter}(1..1\,679, \mathrm{IsSquareFree}))

An exact fraction is a single atom, so length zero

live ↗
InLength(13)\mathrm{Length}(\frac{1}{3})Out00

Complex literals have no parts

live ↗
InLength(2+i)\mathrm{Length}(2+i)Out00

A plain integer has length zero

live ↗
InLength(7)\mathrm{Length}(7)Out00
live ↗
InLength(7)\mathrm{Length}(\sqrt{7})Out00
About
Examples31
Checked againstWolfram Language 29 agree2 differSageMath 21 agree1 differSymPy 18 agree4 differmathlib4 9 agree1 unevaluated8 errorOscar 5 agree
CrosswalkWolfram LanguageSageMathOscar

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

The element count of a finite list; an atom or other non-list expression (ours is 0) fails to elaborate.

mappedoscarpackages/oracle/src/mappings.ts
mappedwolframpackages/oracle/src/mappings.ts

Wolfram's Length of an atom (not a list) is 0, not an error, matching ours; a bare len() raises on a non-list, so it is guarded.

mappedsympypackages/oracle/src/mappings.ts

Wolfram's Length of an atom (not a list) is 0, not an error, matching ours; a bare len() raises on a non-list, so it is guarded. A sympy expression (not a Python list) has no len but counts its own terms via .args (Add(a,b,c,d).args has 4 elements; a bare Symbol's .args is empty, giving 0 like an atom).

mappedsagepackages/oracle/src/mappings.ts

Wolfram's Length of an atom (not a list) is 0, not an error, matching ours; a bare len() raises on a non-list, so it is guarded. A Sage symbolic expression (not a Python list) has no len but counts its own terms via .operands() — a method, unlike sympy's .args tuple (Add(a,b,c,d).operands() has 4 elements; a bare variable's is empty, giving 0 like an atom).

See also: Count