Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Drop elements of a list while a criterion remains true
ResourceFunction["DropWhile"][list,crit] drops elements ei from the beginning of list, continuing so long as crit[ei] is True. | |
ResourceFunction["DropWhile"][crit] represents an operator form of ResourceFunction["DropWhile"] that can be applied to expressions. |
Drop elements while they are even:
In[1]:= | ![]() |
Out[1]= | ![]() |
Use a pure function to drop elements while they are all below 10:
In[2]:= | ![]() |
Out[2]= | ![]() |
Elements of the list can be any expression:
In[3]:= | ![]() |
Out[3]= | ![]() |
Create an operator of DropWhile that drops leading even numbers:
In[4]:= | ![]() |
Out[4]= | ![]() |
Use the operator:
In[5]:= | ![]() |
Out[5]= | ![]() |
Use on an expression with head f:
In[6]:= | ![]() |
Out[6]= | ![]() |
DropWhile also works on SparseArray:
In[7]:= | ![]() |
Out[7]= | ![]() |
Drop leading zeros:
In[8]:= | ![]() |
Out[8]= | ![]() |
DropWhile is equivalent to a combination of Drop and LengthWhile:
In[9]:= | ![]() |
Out[9]= | ![]() |
In[10]:= | ![]() |
Out[10]= | ![]() |
In[11]:= | ![]() |
Out[11]= | ![]() |
DropWhile gives an empty list if crit[ei] is True for all elements:
In[12]:= | ![]() |
Out[12]= | ![]() |
Combining the results of TakeWhile and DropWhile gives the original list:
In[13]:= | ![]() |
Out[14]= | ![]() |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License