TakeSmallestBy[data,f,n]
gives the n elements ei in data for which f[ei] is smallest, sorted in ascending order.
TakeSmallestBy[dataprop,f,n]
gives the property prop for the n elements in data for which f[ei] is smallest.
TakeSmallestBy[data,f,n,p]
uses the ordering function p for sorting.
TakeSmallestBy[f,n]
represents an operator form of TakeSmallestBy that can be applied to an expression.
TakeSmallestBy
TakeSmallestBy[data,f,n]
gives the n elements ei in data for which f[ei] is smallest, sorted in ascending order.
TakeSmallestBy[dataprop,f,n]
gives the property prop for the n elements in data for which f[ei] is smallest.
TakeSmallestBy[data,f,n,p]
uses the ordering function p for sorting.
TakeSmallestBy[f,n]
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 all close allBasic Examples (5)
Give the four numbers closest to zero:
TakeSmallestBy[{-5, -2, 4, 3, 1, 9, 2, -4}, Abs, 4]Take the two shortest strings in a list:
TakeSmallestBy[{"x", "xxx", "xx"}, StringLength, 2]Do the same using the operator form of TakeSmallestBy:
{"x", "xxx", "xx"}//TakeSmallestBy[StringLength, 2]TakeSmallestBy[StringLength, 2][{"x", "xxx", "xx"}]Take three smallest Quantity lengths according to their distance to
:
TakeSmallestBy[{Quantity[0.6, "Meters"], Quantity[0.1, "Meters"], Quantity[0.3, "Meters"], Quantity[0.2, "Meters"]}, Abs[# - Quantity[0.5, "Meters"]]&, 3]Take the three DateObject expressions with the smallest day of the month:
RandomDate[8]TakeSmallestBy[%, #["Day"]&, 3]Take the two shortest strings in an association:
TakeSmallestBy[<|a -> "x", b -> "xxx", c -> "xx"|>, StringLength, 2]Scope (8)
Give the four numbers closest to zero or as many as are available if fewer:
TakeSmallestBy[{-5, 7, 4}, Abs, UpTo[4]]The discovery date value for some planets is missing:
planets = PlanetData[]Map[EntityProperty["Planet", "DiscoveryDate"], planets]By default, Missing[] and several other symbolic expressions are excluded if returned by the selection function:
TakeSmallestBy[planets, EntityProperty["Planet", "DiscoveryDate"], UpTo[3]]Get the two smallest elements by magnitude in a list:
list = {-E^π, 7 π + (I/100), π^E, 23 I};TakeSmallestBy[list -> "Element", Abs, 2]Get the positions of the two smallest elements by magnitude in a list:
TakeSmallestBy[list -> "Index", Abs, 2]Get the two smallest magnitudes in a list:
TakeSmallestBy[list -> "Value", Abs, 2]Get the two smallest elements by magnitude in a list along with their positions:
TakeSmallestBy[list -> {"Element", "Index"}, Abs, 2]Get the two smallest elements by magnitude in a list with their positions given first:
TakeSmallestBy[list -> {"Index", "Element"}, Abs, 2]Get associations containing the element, position and Abs value of the two smallest elements by magnitude in a list:
TakeSmallestBy[list -> All, Abs, 2]words = {"potato", "dream", "sound", "house"};They can be translated to Spanish with WordTranslation, which gives a list of possibilities:
EnglishToSpanish[word_] := First@WordTranslation[word, "English" -> "Spanish"]Map[EnglishToSpanish, words]These are the smallest two words according to canonical order:
TakeSmallest[words, 2]These are the smallest two English words according to canonical order of their Spanish translations:
TakeSmallestBy[words, EnglishToSpanish, 2]Use properties to show simultaneously the English word and its translation to Spanish:
TakeSmallestBy[words -> {"Element", "Value"}, EnglishToSpanish, 2]Construct a TabularColumn object with 100 words:
col = TabularColumn[RandomWord[100]]Select the five shortest words:
TakeSmallestBy[col, StringLength, 5]Normalize the result to a list:
Normal[%]Find the four rows in a Tabular object with smallest values in a given column:
data = {{8, 13, 18, 8, 14, 13, 18, 27}, {16.1, 29.2, 23.8, 14.7, 30.1, 27.5, 35.1, 36.2}};
tab = ToTabular[data, "Columns", {"size", "length"}]TakeSmallestBy[tab, "length", 4]Use general functional notation instead of the column name:
TakeSmallestBy[tab, #length&, 4]TakeSmallestBy[tab, (#length - #size)&, 4]Take a dataset of the solar system planets:
dataset = Dataset[ExampleData[{"Dataset", "Planets"}], MaxItems -> 4]Find the three planets with the smallest number of moons:
TakeSmallestBy[dataset, Length[#Moons]&, 3]//KeysTake a list of integer 3-vectors:
vectors = RandomInteger[{-10, 10}, {5, 3}]Norm /@ vectorsSelect the three vectors with smallest norms, by default sorting numerically those norms:
TakeSmallestBy[vectors, Norm, 3]Sorting the norms by canonical order can give a different result:
TakeSmallestBy[vectors, Norm, 3, Order]Options (2)
ExcludedForms (2)
Take the list of all planets in the solar system:
planets = PlanetData[]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):
close = TakeSmallestBy[planets, AstroDistance, 4, ExcludedForms -> _ ? (LessThan[Quantity[0.1, "AstronomicalUnit"]])]Plot the evolution of the distances during a two-year period centered at the current moment:
distances = Table[{date, AstroDistance[p, date]}, {p, close}, {date, DateRange[Now - Quantity[1, "Years"], Now + Quantity[1, "Years"], Quantity[1, "Weeks"]]}];DateListPlot[distances, PlotLegends -> close]assoc = <|"a" -> 1, "c" -> 2, "d" -> None, "e" -> 3|>Not all these keys have values in the association:
keys = {"a", "b", "c", "d", "e", "f"};Lookup[assoc, keys]Find the keys corresponding up to the four smallest values in the association:
TakeSmallestBy[keys, Lookup[assoc, #]&, UpTo[4]]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:
TakeSmallestBy[{"a", "b", "c", "d", "e", "f"}, Lookup[assoc, #]&, UpTo[4], ExcludedForms -> {_Missing, None}]Specify that no value should be excluded:
TakeSmallestBy[{"a", "b", "c", "d", "e", "f"}, Lookup[assoc, #]&, UpTo[4], ExcludedForms -> {}]Specify that only values None should be excluded:
TakeSmallestBy[{"a", "b", "c", "d", "e", "f"}, Lookup[assoc, #]&, UpTo[4], ExcludedForms -> {None}]Applications (6)
Find the eight capitals of continental US states that are closest to Kansas City:
TakeSmallestBy[EntityValue[EntityClass["AdministrativeDivision", "ContinentalUSStates"], "CapitalCity"], GeoDistance[Entity["City", {"KansasCity", "Missouri", "UnitedStates"}], #]&, 8]GeoGraphics[{GeoPath[{Entity["City", {"KansasCity", "Missouri", "UnitedStates"}], #}]& /@ %, Red, PointSize[Large], Point[%]}, GeoRange -> "Country"]Find the five oldest James Bond movies:
TakeSmallestBy[EntityList@EntityClass["Movie", "JamesBondFranchise"], EntityProperty["Movie", "ReleaseDate"], 5]Find the 10 countries with the shortest names:
TakeSmallestBy[EntityClass["Country", All]["Name"], StringLength, 10]From the BRICS group of countries, give the country that is closest to the current location:
TakeSmallestBy[EntityList[EntityClass["Country", "BRICCountries"]], GeoDistance[#, Here]&, 1]Find the seven US states with the smallest number of neighboring states and show them:
TakeSmallestBy[EntityValue[EntityClass["AdministrativeDivision", "AllUSStatesPlusDC"], EntityProperty["AdministrativeDivision", "BorderingStates"], "EntityAssociation"], Length, 7]Find the five constellations with the smallest number of bright stars, showing how many each one has:
TakeSmallestBy[ConstellationData[] -> All, Length @* EntityProperty["Constellation", "BrightStars"], 5]Properties & Relations (3)
TakeSmallestBy[list,f,n,p] is effectively equivalent to Part[list,TakeSmallest[Map[f,list]"index",n,p]]:
list = {8, 6, -7, 10, 7, 2, -9, 1};TakeSmallestBy[list, Abs, 4]Part[list, TakeSmallest[Map[Abs, list] -> "Index", 4]]TakeSmallestBy[{e1,e2,…},f,n] compares values f[ei] using NumericalOrder by default:
data = {{1, x}, {0, x}, {-Infinity, x}};TakeSmallestBy[data, First, 2]Sort[First /@ data, NumericalOrder]MinimalBy[{e1,e2,…},f,n] compares values f[ei] using canonical Order by default:
MinimalBy[data, First, 2]Sort[First /@ data, Order]Both TakeSmallestBy and MinimalBy take an ordering function as fourth argument, which makes them effectively equivalent:
TakeSmallestBy[data, First, 2, Order] === MinimalBy[data, First, 2]MinimalBy[data, First, 2, NumericalOrder] === TakeSmallestBy[data, First, 2]When there are common values of f[ei] for different elements ei in TakeSmallestBy[{e1,e2,…},f,n], the original order will be kept:
list = Range[-5, 5]TakeSmallestBy[list, Abs, 4]TakeSmallestBy[Reverse[list], Abs, 4]Possible Issues (2)
If fewer than the requested number of elements are present, TakeSmallestBy will not evaluate:
TakeSmallestBy[{3, 1, 2}, f, 5]Use UpTo to get as many elements as possible:
TakeSmallestBy[{3, 1, 2}, f, UpTo[5]]If the f[ei] are not comparable, TakeSmallestBy will not evaluate:
TakeSmallestBy[{Quantity[1, "Hour"], Quantity[5, "Feet"]}, UnitConvert, 1]See Also
TakeSmallest MinimalBy Min PositionSmallest SortBy Sort NumericalSort NumericalOrder Ordering RankedMin TakeLargest TakeLargestBy MaximalBy DeleteCases
Function Repository: DropSmallestBy
Related Guides
Text
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.
APA
Wolfram Language. (2015). TakeSmallestBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TakeSmallestBy.html
BibTeX
@misc{reference.wolfram_2026_takesmallestby, author="Wolfram Research", title="{TakeSmallestBy}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/TakeSmallestBy.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_takesmallestby, organization={Wolfram Research}, title={TakeSmallestBy}, year={2025}, url={https://reference.wolfram.com/language/ref/TakeSmallestBy.html}, note=[Accessed: 13-June-2026]}