Length
The number of elements in the collection.
Length!Length(collection)the number of elements in the collection.enumeratio_length(collection)Oscarlength(collection)Length(expr)the number of top-level operands of any expression, not just a collection.enumeratio_length(expr)Oscarlength(expr)Length(finset)the number of members of a Finset, its cardinality.enumeratio_length(finset)Oscarlength(finset)Typed signatures
| Type | Package | Applies |
|---|---|---|
(any) -> infinity | integer | combinatoricsreplaces compute-engine | always |
(any) -> infinity | integer | compute-engine | always |
Details
- Works on any collection head, not just — e.g. .
- Additive over concatenation: . 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
Cardinalityhead, so this is its cardinality.
Examples
1 more kept as data, checked against the oraclesScope #
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 ↗About
| Examples | 31 |
|---|---|
| Checked against | Wolfram Language 29 agree2 differSageMath 21 agree1 differSymPy 18 agree4 differmathlib4 9 agree1 unevaluated8 errorOscar 5 agree |
| Crosswalk | Wolfram LanguageSageMathOscar |
Implementation
mathlib4packages/oracle/src/mappings.tsThe element count of a finite list; an atom or other non-list expression (ours is 0) fails to elaborate.
oscarpackages/oracle/src/mappings.tswolframpackages/oracle/src/mappings.tsWolfram'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.
sympypackages/oracle/src/mappings.tsWolfram'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).
sagepackages/oracle/src/mappings.tsWolfram'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