TakeSmallestBy
✖
TakeSmallestBy
gives the n elements ei in data for which f[ei] is smallest, sorted in ascending order.
gives the property prop for the n elements in data for which f[ei] is smallest.
represents an operator form of TakeSmallestBy that can be applied to an expression.
Details and Options


- TakeSmallestBy gives the elements ei in the first level of the input data corresponding to the smallest f[ei] values.
- By default, TakeSmallestBy uses NumericalOrder to sort f[ei] values that are numeric expressions, Quantity objects and DateObject expressions. For other types of objects, TakeSmallestBy sorts the f[ei] values by canonical Order.
- The data can have the following forms:
-
{e1,e2,…} list of values, including numbers, quantities, dates, ... Association[…] association of values » QuantityArray[…] quantity array or other structured array Tabular[…] type-consistent tabular data » TabularColumn[…] type-consistent column data » Dataset[…] general hierarchical data » - TakeSmallestBy[assoc,f,n] gives an association of length n by taking the values in assoc that are smallest according to f, preserving their keys.
- For tabular data tab, TakeSmallestBy[tab,f,…] applies the function f to individual rows of tab, with the row being an association <col1val1,… > if tab has column keys or a list {val1,…} if tab does not have column keys.
- In TakeSmallestBy[dataprop,f,n], possible forms for prop include:
-
"Element" gives each element itself » "Index" gives the index for each element » "Value" gives the value f[x] for each element x » {prop1,prop2,…} a list of multiple forms » All gives an association with element, index and f value » - TakeSmallestBy[data,f,UpTo[n]] takes n elements or as many as are available. »
- TakeSmallestBy[f,n][data] is equivalent to TakeSmallestBy[data,f,n].
- TakeSmallestBy has option ExcludedForms. With the default setting ExcludedForms->Automatic, TakeSmallestBy excludes from its final results elements for which f[ei] is None, Null or Indeterminate or has head Missing. »
- The setting ExcludedForms->{patt1,patt2,…} specifies that expressions for which f[ei] matches any of the patti should be excluded from results generated by TakeSmallestBy. »
Examples
open allclose allBasic Examples (5)Summary of the most common use cases
Give the four numbers closest to zero:

https://wolfram.com/xid/0cpr7b0syb67il-quxmyv

Take the two shortest strings in a list:

https://wolfram.com/xid/0cpr7b0syb67il-xh63mx

Do the same using the operator form of TakeSmallestBy:

https://wolfram.com/xid/0cpr7b0syb67il-cvc29l


https://wolfram.com/xid/0cpr7b0syb67il-vm8bxx

Take three smallest Quantity lengths according to their distance to :

https://wolfram.com/xid/0cpr7b0syb67il-6t1czq

Take the three DateObject expressions with the smallest day of the month:

https://wolfram.com/xid/0cpr7b0syb67il-rfaq8r


https://wolfram.com/xid/0cpr7b0syb67il-3oy020

Take the two shortest strings in an association:

https://wolfram.com/xid/0cpr7b0syb67il-jgfph4

Scope (8)Survey of the scope of standard use cases
Give the four numbers closest to zero or as many as are available if fewer:

https://wolfram.com/xid/0cpr7b0syb67il-0pcpdc

The discovery date value for some planets is missing:

https://wolfram.com/xid/0cpr7b0syb67il-il3p2i


https://wolfram.com/xid/0cpr7b0syb67il-oh3twu

By default, Missing[] and several other symbolic expressions are excluded if returned by the selection function:

https://wolfram.com/xid/0cpr7b0syb67il-c91uu4

Get the two smallest elements by magnitude in a list:

https://wolfram.com/xid/0cpr7b0syb67il-1p39n0

https://wolfram.com/xid/0cpr7b0syb67il-5yfcey

Get the positions of the two smallest elements by magnitude in a list:

https://wolfram.com/xid/0cpr7b0syb67il-cr2dko

Get the two smallest magnitudes in a list:

https://wolfram.com/xid/0cpr7b0syb67il-gpmais

Get the two smallest elements by magnitude in a list along with their positions:

https://wolfram.com/xid/0cpr7b0syb67il-i70n6a

Get the two smallest elements by magnitude in a list with their positions given first:

https://wolfram.com/xid/0cpr7b0syb67il-dw04qo

Get associations containing the element, position and Abs value of the two smallest elements by magnitude in a list:

https://wolfram.com/xid/0cpr7b0syb67il-21mfoi


https://wolfram.com/xid/0cpr7b0syb67il-gd1pkd
They can be translated to Spanish with WordTranslation, which gives a list of possibilities:

https://wolfram.com/xid/0cpr7b0syb67il-ufmytu

https://wolfram.com/xid/0cpr7b0syb67il-z63mfq

These are the smallest two words according to canonical order:

https://wolfram.com/xid/0cpr7b0syb67il-6zdtb7

These are the smallest two English words according to canonical order of their Spanish translations:

https://wolfram.com/xid/0cpr7b0syb67il-4utuab

Use properties to show simultaneously the English word and its translation to Spanish:

https://wolfram.com/xid/0cpr7b0syb67il-gkizxz

Construct a TabularColumn object with 100 words:

https://wolfram.com/xid/0cpr7b0syb67il-ss77bg

Select the five shortest words:

https://wolfram.com/xid/0cpr7b0syb67il-ije4w6

Normalize the result to a list:

https://wolfram.com/xid/0cpr7b0syb67il-5kepgn

Find the four rows in a Tabular object with smallest values in a given column:

https://wolfram.com/xid/0cpr7b0syb67il-jpfbrr


https://wolfram.com/xid/0cpr7b0syb67il-y5poc

Use general functional notation instead of the column name:

https://wolfram.com/xid/0cpr7b0syb67il-2shaqt


https://wolfram.com/xid/0cpr7b0syb67il-r1yvkm

Take a dataset of the solar system planets:

https://wolfram.com/xid/0cpr7b0syb67il-2sxsoh

Find the three planets with the smallest number of moons:

https://wolfram.com/xid/0cpr7b0syb67il-3981jc

Take a list of integer 3-vectors:

https://wolfram.com/xid/0cpr7b0syb67il-pyk5yx


https://wolfram.com/xid/0cpr7b0syb67il-uzhb9e

Select the three vectors with smallest norms, by default sorting numerically those norms:

https://wolfram.com/xid/0cpr7b0syb67il-9chmoz

Sorting the norms by canonical order can give a different result:

https://wolfram.com/xid/0cpr7b0syb67il-266zb3

Options (2)Common values & functionality for each option
ExcludedForms (2)
Take the list of all planets in the solar system:

https://wolfram.com/xid/0cpr7b0syb67il-o9kj4i

Find the four planets that are currently closest to your location, with a lower limit of .1 au in distance (to exclude the location planet):

https://wolfram.com/xid/0cpr7b0syb67il-o1c69u

Plot the evolution of the distances during a two-year period centered at the current moment:

https://wolfram.com/xid/0cpr7b0syb67il-ejty5f

https://wolfram.com/xid/0cpr7b0syb67il-bb1ah6


https://wolfram.com/xid/0cpr7b0syb67il-gg93zh

Not all these keys have values in the association:

https://wolfram.com/xid/0cpr7b0syb67il-wkaz6w

https://wolfram.com/xid/0cpr7b0syb67il-wna32b

Find the keys corresponding up to the four smallest values in the association:

https://wolfram.com/xid/0cpr7b0syb67il-bonkqh

By default, some symbolic objects like Missing[] or None are excluded as results of the selection function, so the previous result is equivalent to this:

https://wolfram.com/xid/0cpr7b0syb67il-9kqzz5

Specify that no value should be excluded:

https://wolfram.com/xid/0cpr7b0syb67il-rjnsp0

Specify that only values None should be excluded:

https://wolfram.com/xid/0cpr7b0syb67il-fy7rmm

Applications (6)Sample problems that can be solved with this function
Find the eight capitals of continental US states that are closest to Kansas City:

https://wolfram.com/xid/0cpr7b0syb67il-et8buy


https://wolfram.com/xid/0cpr7b0syb67il-66q9wg

Find the five oldest James Bond movies:

https://wolfram.com/xid/0cpr7b0syb67il-lgvrjt

Find the 10 countries with the shortest names:

https://wolfram.com/xid/0cpr7b0syb67il-fcthyl

From the BRICS group of countries, give the country that is closest to the current location:

https://wolfram.com/xid/0cpr7b0syb67il-wfok81

Find the seven US states with the smallest number of neighboring states and show them:

https://wolfram.com/xid/0cpr7b0syb67il-f0nqbi

Find the five constellations with the smallest number of bright stars, showing how many each one has:

https://wolfram.com/xid/0cpr7b0syb67il-y9s8ve

Properties & Relations (3)Properties of the function, and connections to other functions
TakeSmallestBy[list,f,n,p] is effectively equivalent to Part[list,TakeSmallest[Map[f,list]"index",n,p]:

https://wolfram.com/xid/0cpr7b0syb67il-iezsol

https://wolfram.com/xid/0cpr7b0syb67il-dsovx7


https://wolfram.com/xid/0cpr7b0syb67il-s7dcf5

TakeSmallestBy[{e1,e2,…},f,n] compares values f[ei] using NumericalOrder by default:

https://wolfram.com/xid/0cpr7b0syb67il-ddbd7p

https://wolfram.com/xid/0cpr7b0syb67il-dfljwn


https://wolfram.com/xid/0cpr7b0syb67il-82pmav

MinimalBy[{e1,e2,…},f,n] compares values f[ei] using canonical Order by default:

https://wolfram.com/xid/0cpr7b0syb67il-d1gqp2


https://wolfram.com/xid/0cpr7b0syb67il-trdyiu

Both TakeSmallestBy and MinimalBy take an ordering function as fourth argument, which makes them effectively equivalent:

https://wolfram.com/xid/0cpr7b0syb67il-0ihvu1


https://wolfram.com/xid/0cpr7b0syb67il-mgg1cj

When there are common values of f[ei] for different elements ei in TakeSmallestBy[{e1,e2,…},f,n], the original order will be kept:

https://wolfram.com/xid/0cpr7b0syb67il-eie171


https://wolfram.com/xid/0cpr7b0syb67il-7xt8dc


https://wolfram.com/xid/0cpr7b0syb67il-3qjjjv

Possible Issues (2)Common pitfalls and unexpected behavior
If fewer than the requested number of elements are present, TakeSmallestBy will not evaluate:

https://wolfram.com/xid/0cpr7b0syb67il-wv21bw


Use UpTo to get as many elements as possible:

https://wolfram.com/xid/0cpr7b0syb67il-pu5pzo

If the f[ei] are not comparable, TakeSmallestBy will not evaluate:

https://wolfram.com/xid/0cpr7b0syb67il-b0vssr


Wolfram Research (2015), TakeSmallestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeSmallestBy.html (updated 2025).
Text
Wolfram Research (2015), TakeSmallestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeSmallestBy.html (updated 2025).
Wolfram Research (2015), TakeSmallestBy, Wolfram Language function, https://reference.wolfram.com/language/ref/TakeSmallestBy.html (updated 2025).
CMS
Wolfram Language. 2015. "TakeSmallestBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/TakeSmallestBy.html.
Wolfram Language. 2015. "TakeSmallestBy." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/TakeSmallestBy.html.
APA
Wolfram Language. (2015). TakeSmallestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeSmallestBy.html
Wolfram Language. (2015). TakeSmallestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeSmallestBy.html
BibTeX
@misc{reference.wolfram_2025_takesmallestby, author="Wolfram Research", title="{TakeSmallestBy}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/TakeSmallestBy.html}", note=[Accessed: 06-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_takesmallestby, organization={Wolfram Research}, title={TakeSmallestBy}, year={2025}, url={https://reference.wolfram.com/language/ref/TakeSmallestBy.html}, note=[Accessed: 06-June-2025
]}