Wolfram Research

Function Repository Resource:

DropSmallestBy

Source Notebook

Drop the smallest values from a list based on a function

Contributed by: Sander Huisman

ResourceFunction["DropSmallestBy"][list,f,n]

drops the n elements ei from list for which f[ei] is smallest.

ResourceFunction["DropSmallestBy"][f,n]

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

Examples

Basic Examples

Drop the smallest two values determined by their magnitude:

In[1]:=
ResourceFunction["DropSmallestBy"][{1, -5, 3, -8, -4, 2, 9}, Abs, 2]
Out[1]=

Drop the smallest two complex values determined by their magnitude:

In[2]:=
ResourceFunction[
 "DropSmallestBy"][{1 + I, -5, 3 I, -I - 2, -4, 1.3 I, 9}, Abs, 2]
Out[2]=

Use the operator form by first creating an operator op:

In[3]:=
op = ResourceFunction["DropSmallestBy"][Abs, 2]
Out[3]=

Apply the operator:

In[4]:=
op[{1, -5, 3, -8, -4, 2, 9}]
Out[4]=

Scope

When n is 0 nothing is deleted:

In[5]:=
ResourceFunction["DropSmallestBy"][{1, 5, 3, 8, 4, 2, 9}, Abs, 0]
Out[5]=

A negative number deletes the largest n numbers:

In[6]:=
ResourceFunction["DropSmallestBy"][{1, -5, 3, -8, -4, 2, 9}, Abs, -2]
Out[6]=

Properties and Relations

If n is larger than the length of the list then an empty list is returned:

In[7]:=
ResourceFunction["DropSmallestBy"][{1, -5, 3, -8, -4, 2, 9}, Sign, 12]
Out[7]=

DropSmallestBy and TakeSmallestBy are complementary:

In[8]:=
in = {1, -5, 3, -8, -4, 2, 9};
In[9]:=
ResourceFunction["DropSmallestBy"][in, Abs, 3]
Out[9]=
In[10]:=
TakeSmallestBy[in, Abs, 3]
Out[10]=

Resource History

Related Resources

License Information