Function Repository Resource:

TakeUntil

Source Notebook

Take elements from a list until an element satisfies a test

Contributed by: E. Chan-López & Jorge Luis Ramos Castellano

ResourceFunction["TakeUntil"][list,test]

takes elements from list until the first element that satisfies test. If no element satisfies test or if the element that satisfies the test is the last element in the list, it returns the entire list.

Details

ResourceFunction["TakeUntil"] can be used on an object with any head, not necessarily List.

Examples

Basic Examples (2) 

Take all elements until an odd number is encountered:

In[1]:=
ResourceFunction["TakeUntil"][{2, 4, 6, 1, 2, 3}, OddQ]
Out[1]=

Use a pure function to take all elements until the first element above 10:

In[2]:=
ResourceFunction["TakeUntil"][{1, 1, 2, 3, 5, 8, 13, 21}, # > 10 &]
Out[2]=

Scope (4) 

Elements of the list can be any expression:

In[3]:=
ResourceFunction[
 "TakeUntil"][{a, Pi, 3, 2, {1, 2, 3}, {a, b}, 10}, ListQ]
Out[3]=

Use TakeUntil to take elements from a list until the first element that satisfies the given test is found:

In[4]:=
ResourceFunction[
 "TakeUntil"][{0, 0, 0, 0, 3, 5, 1, 7, 0, 0, 0}, # != 0 &]
Out[4]=

By default, TakeUntil returns the entire list when no element satisfies the condition:

In[5]:=
ResourceFunction["TakeUntil"][{1, 2, 3, 4, 5, 6, 7, 8, 9}, StringQ]
Out[5]=

TakeUntil returns the entire list when the element that satisfies the test is the last one in the list:

In[6]:=
ResourceFunction["TakeUntil"][{1, 2, 3, 5, 8, 13, 21, a}, MatchQ[#, _Symbol] &]
Out[6]=

Generalizations & Extensions (1) 

Use TakeUntil on an expression with head f:

In[7]:=
ResourceFunction["TakeUntil"][f[1, 1, 2, 3, a, 8, 13, 21], Head[#] === Symbol &]
Out[7]=

Applications (1) 

Find the digits of GoldenRatio up to and including the first occurrence of a 7:

In[8]:=
ResourceFunction["TakeUntil"][
 First[RealDigits[GoldenRatio, 10, 100]], # == 7 &]
Out[8]=

Properties & Relations (5) 

When the test for TakeUntil is the logical negation of the test for TakeWhile, the result of TakeWhile is always contained within the result of TakeUntil. For example:

In[9]:=
SubsetQ[ResourceFunction["TakeUntil"][{2, 4, 6, 1, 2, 3}, EvenQ], TakeWhile[{2, 4, 6, 1, 2, 3}, OddQ]]
Out[9]=

If the test function always returns False, TakeUntil will return the entire list. For example:

In[10]:=
ResourceFunction["TakeUntil"][{2, 4, 6, 1, 2, 3}, False &]
Out[10]=

When using TakeUntil with an Association, it operates on the Values:

In[11]:=
ResourceFunction[
 "TakeUntil"][<|1 -> 1, 2 -> 3, 3 -> 5, 4 -> 2, 5 -> e, 6 -> f|>, EvenQ]
Out[11]=

Use TakeUntil with the resource function SymbolQ:

In[12]:=
ResourceFunction["TakeUntil"][{1, "2", 3, "4", C, a, K}, ResourceFunction["SymbolQ"]]
Out[12]=

Use TakeUntil with the resource function PossibleNameQ:

In[13]:=
ResourceFunction[
 "TakeUntil"][{1, "2", 3, "4", "System!`Plot", "x", a, K}, ResourceFunction["PossibleNameQ"]]
Out[13]=

Possible Issues (1) 

If the criterion does not return strictly boolean values (True or False), TakeUntil will treat any non-True value as False. For example:

In[14]:=
ResourceFunction["TakeUntil"][{8, a, b, 20, 1.4}, Echo[# > 15] &]
Out[14]=

Neat Examples (1) 

Use TakeUntil on a list of integer pairs, with a condition to stop at the first pair where the total exceeds a specified threshold:

In[15]:=
pairs = {{2, 1}, {3, 4}, {2, 2}, {3, 1}, {4, 2}, {7, 3}, {7, 4}, {8, 5}, {9, 7}};
In[16]:=
ResourceFunction["TakeUntil"][pairs, Total@# > 10 &]
Out[16]=

Publisher

Ramón Eduardo Chan López

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 14 February 2025

Related Resources

License Information