WOLFRAM

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 allclose all

Basic Examples  (1)Summary of the most common use cases

Find the length of the run of elements below 10:

Out[1]=1

Scope  (1)Survey of the scope of standard use cases

Elements of the list can be any expression:

Out[1]=1

Generalizations & Extensions  (1)Generalized and extended use cases

Use on an expression with head f:

Out[1]=1

The result is the same as for a list of the values:

Out[2]=2

Applications  (1)Sample problems that can be solved with this function

Find the length of the run of digits in before a 7 occurs:

Out[1]=1

Properties & Relations  (3)Properties of the function, and connections to other functions

LengthWhile gives the length of the result from TakeWhile:

Out[1]=1
Out[2]=2

LengthWhile is equal to Length if crit[ei] is True for all elements:

Out[1]=1
Out[2]=2

Otherwise it is 1 less than the first position for which crit[ei] is not True:

Out[1]=1
Out[2]=2
Out[3]=3

Possible Issues  (1)Common pitfalls and unexpected behavior

The count terminates for any value of crit[ei] that is not True:

Out[1]=1
Out[2]=2
Wolfram Research (2007), LengthWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/LengthWhile.html (updated 2008).
Wolfram Research (2007), LengthWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/LengthWhile.html (updated 2008).

Text

Wolfram Research (2007), LengthWhile, Wolfram Language function, https://reference.wolfram.com/language/ref/LengthWhile.html (updated 2008).

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.

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

Wolfram Language. (2007). LengthWhile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LengthWhile.html

BibTeX

@misc{reference.wolfram_2025_lengthwhile, author="Wolfram Research", title="{LengthWhile}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/LengthWhile.html}", note=[Accessed: 29-March-2025 ]}

@misc{reference.wolfram_2025_lengthwhile, author="Wolfram Research", title="{LengthWhile}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/LengthWhile.html}", note=[Accessed: 29-March-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_lengthwhile, organization={Wolfram Research}, title={LengthWhile}, year={2008}, url={https://reference.wolfram.com/language/ref/LengthWhile.html}, note=[Accessed: 29-March-2025 ]}

@online{reference.wolfram_2025_lengthwhile, organization={Wolfram Research}, title={LengthWhile}, year={2008}, url={https://reference.wolfram.com/language/ref/LengthWhile.html}, note=[Accessed: 29-March-2025 ]}