Wolfram Research

Function Repository Resource:

TakeSmaller

Source Notebook

Take values smaller than a threshold in a list or association

Contributed by: Wolfram Staff

ResourceFunction["TakeSmaller"][list,val,n]

gives the largest n elements in list that are smaller than val.

ResourceFunction["TakeSmaller"][listprop,val,n]

gives the property prop for the n elements in list that are smaller than val.

ResourceFunction["TakeSmaller"][val,n]

represents an operator form of ResourceFunction["TakeSmaller"] that can be applied to an expression.

Details and Options

ResourceFunction["TakeSmaller"] sorts by numerical magnitude.
ResourceFunction["TakeSmaller"] handles the same objects and properties as TakeSmallest.
ResourceFunction["TakeSmaller"] expects all objects to be comparable with one another.
ResourceFunction["TakeSmaller"][list,val,UpTo[n]] takes n elements, or as many as are available.
ResourceFunction["TakeSmaller"][assoc,val,n] gives an association of length n by taking the n largest values that are smaller then val in assoc, preserving their keys.
ResourceFunction["TakeSmaller"][val,n][list] is equivalent to ResourceFunction["TakeSmaller"][list,val,n].

Examples

Basic Examples

Take two numbers smaller than the specified value in a list:

In[1]:=
list = {1, 3, 0, 4, 5}
Out[1]=
In[2]:=
ResourceFunction["TakeSmaller"][list, 4, 2]
Out[2]=

Do the same using the operator form of TakeSmaller:

In[3]:=
list // ResourceFunction["TakeSmaller"][4, 2]
Out[3]=
In[4]:=
ResourceFunction["TakeSmaller"][4, 2] @ list
Out[4]=

Take the largest two values smaller than the specified threshold in an association:

In[5]:=
ResourceFunction[
 "TakeSmaller"][<|a -> 0, b -> 1, c -> 2, d -> 5, e -> 4|>, 3, 2]
Out[5]=

Take the largest four numbers smaller than the specified value, or as many as are available:

In[6]:=
ResourceFunction["TakeSmaller"][{4, 7, 1, 3}, 5, UpTo[4]]
Out[6]=

Take all numbers with values under the threshold:

In[7]:=
ResourceFunction["TakeSmaller"][{1, 7, 4, 5}, 5, UpTo[Infinity]]
Out[7]=

Take two values in a list of Quantity objects:

In[8]:=
ResourceFunction[
 "TakeSmaller"][{Quantity[100, "Grams"], Quantity[2, "Kilograms"], Quantity[3, "Kilograms"], Quantity[4, "Kilograms"]}, Quantity[3, "Kilograms"], 2]
Out[8]=

Scope

Get the two largest elements that are smaller than 23 in a list:

In[9]:=
ResourceFunction["TakeSmaller"][{E^\[Pi], 7 \[Pi], \[Pi]^E, 23}, 23,
  2]
Out[9]=

Get the positions of the two largest elements smaller than 3:

In[10]:=
list = {E^\[Pi], 7 \[Pi], \[Pi]^E, 23};
In[11]:=
ResourceFunction["TakeSmaller"][list -> "Index", 23, 2]
Out[11]=

Get the elements along with their positions:

In[12]:=
ResourceFunction["TakeSmaller"][list -> {"Element", "Index"}, 23, 2]
Out[12]=

Let positions come first in the result:

In[13]:=
ResourceFunction["TakeSmaller"][list -> {"Index", "Element"}, 23, 2]
Out[13]=

Get associations containing the element and the position:

In[14]:=
ResourceFunction["TakeSmaller"][list -> All, 23, 2]
Out[14]=

Get the two largest planets with radii smaller than Earth's:

In[15]:=
ResourceFunction["TakeSmaller"][
 EntityValue["Planet", "Radius", "EntityAssociation"], Entity["Planet", "Earth"]["Radius"], 2]
Out[15]=

Resource History

Related Resources

License Information