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

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

When n=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 (2) 

If n is larger than the length of the list, 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]=

Publisher

SHuisman

Version History

  • 1.0.0 – 02 April 2020

Related Resources

License Information