NestWhileList
✖
NestWhileList
generates a list of the results of applying f repeatedly, starting with expr, and continuing until applying test to the result no longer yields True.
supplies the most recent m results as arguments for test at each step.
Details

- The last element of the list returned by NestWhileList[f,expr,test] is always an expression to which applying test does not yield True.
- NestWhileList[f,expr,test,m] at each step evaluates test[res1,res2,…,resm]. It does not put the results resi in a list.
- The resi are given in the order they are generated, with the most recent coming last.
- NestWhileList[f,expr,test,m] does not start applying test until at least m results have been generated. »
- NestWhileList[f,expr,test,{mmin,m}] does not start applying test until at least mmin results have been generated. At each step it then supplies as arguments to test as many recent results as possible, up to a maximum of m. »
- NestWhileList[f,expr,test,m] is equivalent to NestWhileList[f,expr,test,{m,m}]. »
- NestWhileList[f,expr,UnsameQ,2] is equivalent to FixedPointList[f,expr]. »
- NestWhileList[f,expr,test,All] is equivalent to NestWhileList[f,expr,test,{1,Infinity}]. »
- NestWhileList[f,expr,UnsameQ,All] goes on applying f until the same result first appears more than once.
- NestWhileList[f,expr,test,m,max,n] applies f an extra n times, appending the results to the list generated. »
- NestWhileList[f,expr,test,m,max,-n] drops the last n elements from the list generated. »
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Scope (4)Survey of the scope of standard use cases
Start comparisons after 4 iterations, and compare using the 4 last values:

https://wolfram.com/xid/0dqw6k228vu-t2u457

Start comparisons after 4 iterations, and compare using the 6 last values:

https://wolfram.com/xid/0dqw6k228vu-zhom6v

Always compare all values generated:

https://wolfram.com/xid/0dqw6k228vu-k60s56

Stop after at most 4 steps, even if the condition is still True:

https://wolfram.com/xid/0dqw6k228vu-l8lha4

Generalizations & Extensions (1)Generalized and extended use cases
Continue until the result is no longer greater than 1:

https://wolfram.com/xid/0dqw6k228vu-0bqu8h

Perform one more step after the condition is no longer True:

https://wolfram.com/xid/0dqw6k228vu-8xuhnc

Drop the last value generated (for which the test was no longer True):

https://wolfram.com/xid/0dqw6k228vu-ev4bxa

Applications (5)Sample problems that can be solved with this function
Find successive integers until a prime is reached:

https://wolfram.com/xid/0dqw6k228vu-k1v

Find the multiplicative order of 2 modulo 19:

https://wolfram.com/xid/0dqw6k228vu-ev


https://wolfram.com/xid/0dqw6k228vu-fdhyhu

Use MultiplicativeOrder to compute directly:

https://wolfram.com/xid/0dqw6k228vu-c53rl0

Find the orbit of under the mapping
:

https://wolfram.com/xid/0dqw6k228vu-fd5

Keep applying iterations in the problem until the results repeat:

https://wolfram.com/xid/0dqw6k228vu-rld

Exclude the first repeating element from the output:

https://wolfram.com/xid/0dqw6k228vu-jzn4n5

Apply Newton iterations for until successive results are within 0.001:

https://wolfram.com/xid/0dqw6k228vu-b4bmxr

Properties & Relations (3)Properties of the function, and connections to other functions
These two forms are equivalent:

https://wolfram.com/xid/0dqw6k228vu-pvvciq


https://wolfram.com/xid/0dqw6k228vu-3jni4a

NestWhileList returns all intermediate values of NestWhile:

https://wolfram.com/xid/0dqw6k228vu-eg6ff6


https://wolfram.com/xid/0dqw6k228vu-bb0h1c

FixedPointList always compares the last two values; these two forms are equivalent:

https://wolfram.com/xid/0dqw6k228vu-q9ips1


https://wolfram.com/xid/0dqw6k228vu-qmo6dq

Neat Examples (2)Surprising or curious use cases

https://wolfram.com/xid/0dqw6k228vu-pfk1xm

Distance of two vertices in a graph:

https://wolfram.com/xid/0dqw6k228vu-nk80px

https://wolfram.com/xid/0dqw6k228vu-c4bflh

https://wolfram.com/xid/0dqw6k228vu-ggy70w


https://wolfram.com/xid/0dqw6k228vu-em0qx6

Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.
Text
Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.
Wolfram Research (1999), NestWhileList, Wolfram Language function, https://reference.wolfram.com/language/ref/NestWhileList.html.
CMS
Wolfram Language. 1999. "NestWhileList." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhileList.html.
Wolfram Language. 1999. "NestWhileList." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestWhileList.html.
APA
Wolfram Language. (1999). NestWhileList. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestWhileList.html
Wolfram Language. (1999). NestWhileList. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestWhileList.html
BibTeX
@misc{reference.wolfram_2025_nestwhilelist, author="Wolfram Research", title="{NestWhileList}", year="1999", howpublished="\url{https://reference.wolfram.com/language/ref/NestWhileList.html}", note=[Accessed: 22-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_nestwhilelist, organization={Wolfram Research}, title={NestWhileList}, year={1999}, url={https://reference.wolfram.com/language/ref/NestWhileList.html}, note=[Accessed: 22-May-2025
]}