Wolfram Research

Function Repository Resource:

DropLargestBy

Source Notebook

Drop the largest values from a list based on a function

Contributed by: Sander Huisman

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

drops the n elements ei in list for which f[ei] is largest.

ResourceFunction["DropLargestBy"][f,n]

represents an operator form of DropLargestBy.

Examples

Basic Examples

Drop the largest two values:

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

Use the operator form by first creating an operator op:

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

Apply the operator:

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

Scope

With n=0 nothing is deleted:

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

A negative number deletes the smallest n numbers:

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

Properties and Relations

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

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

DropLargestBy is complementary to TakeLargestBy:

In[7]:=
in = {1, -5, 3, -8, -4, 2, 9};
In[8]:=
ResourceFunction["DropLargestBy"][in, Abs, 3]
Out[8]=
In[9]:=
TakeLargestBy[in, Abs, 3]
Out[9]=

Resource History

Related Resources

License Information