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 ResourceFunction["DropLargestBy"].

Examples

Basic Examples (2) 

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 (2) 

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 (2) 

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]=

Publisher

SHuisman

Version History

  • 1.0.0 – 02 April 2020

Related Resources

License Information