LengthWhile[list,crit]
gives the number of contiguous elements ei starting at the beginning of list for which crit[ei] is True.
LengthWhile
LengthWhile[list,crit]
gives the number of contiguous elements ei starting at the beginning of list for which crit[ei] is True.
Details
- LengthWhile can be used on an object with any head, not necessarily List.
Examples
open all close allBasic Examples (1)
Scope (1)
Generalizations & Extensions (1)
Applications (1)
Properties & Relations (3)
LengthWhile gives the length of the result from TakeWhile:
TakeWhile[{1, 1, 2, 3, 5, 8, 13, 21}, # < 10&]LengthWhile[{1, 1, 2, 3, 5, 8, 13, 21}, # < 10&]LengthWhile is equal to Length if crit[ei] is True for all elements:
LengthWhile[{1, 1, 2, 3, 5, 8, 13, 21}, NumberQ]Length[{1, 1, 2, 3, 5, 8, 13, 21}]Otherwise it is 1 less than the first position for which crit[ei] is not True:
data = {1, 1, 2, 3, 5, 8, 13, 21}LengthWhile[data, OddQ]Position[Map[OddQ, data], _ ? (Not[TrueQ[#]]&), Heads -> False][[1, 1]] - 1Possible Issues (1)
The count terminates for any value of crit[ei] that is not True:
LengthWhile[{E, 8, a, b, 20, 1.4}, If[NumericQ[#], True, #]&]Map[If[NumericQ[#], True, #]&, {E, 8, a, b, 20, 1.4}]See Also
Tech Notes
Related Guides
Text
Wolfram Research (2007), LengthWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/LengthWhile.html (updated 2008).
CMS
Wolfram Language. 2007. "LengthWhile." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2008. https://reference.wolfram.com/language/ref/LengthWhile.html.
APA
Wolfram Language. (2007). LengthWhile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LengthWhile.html
BibTeX
@misc{reference.wolfram_2026_lengthwhile, author="Wolfram Research", title="{LengthWhile}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/LengthWhile.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_lengthwhile, organization={Wolfram Research}, title={LengthWhile}, year={2008}, url={https://reference.wolfram.com/language/ref/LengthWhile.html}, note=[Accessed: 12-June-2026]}