Skip to content

Commit 76690ae

Browse files
committed
added documentation and blurb
1 parent 805a332 commit 76690ae

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Doc/howto/sorting.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ library provides several tools that do less work than a full sort:
400400
respectively. These functions make a single pass over the input data and
401401
require almost no auxiliary memory.
402402

403+
* :func:`minmax` returns both the smallest and largest values,
404+
respectively. This functions make a single pass over the input data and
405+
require almost no auxiliary memory. This function is useful if both `min` and
406+
`max` need to be called and thus is more efficient for larger datasets.
407+
403408
* :func:`heapq.nsmallest` and :func:`heapq.nlargest` return
404409
the *n* smallest and largest values, respectively. These functions
405410
make a single pass over the data keeping only *n* elements in memory

Doc/library/functions.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,28 @@ are always available. They are listed here in alphabetical order.
12951295
.. versionchanged:: 3.8
12961296
The *key* can be ``None``.
12971297

1298+
.. function:: minmax(iterable, /, *, key=None)
1299+
minmax(iterable, /, *, default, key=None)
1300+
minmax(arg1, arg2, /, *args, key=None)
1301+
1302+
Return the smallest and largest items respectively in an iterable or the smallest and largest
1303+
of two or more arguments.
1304+
1305+
If one positional argument is provided, it should be an :term:`iterable`.
1306+
The smallest and largest items in the iterable are returned. If two or more positional
1307+
arguments are provided, the smallest and largest of the positional arguments are
1308+
returned.
1309+
1310+
There are two optional keyword-only arguments. The *key* argument specifies
1311+
a one-argument ordering function like that used for :meth:`list.sort`. The
1312+
*default* argument specifies an object to return if the provided iterable is
1313+
empty. If the iterable is empty and *default* is not provided, a
1314+
:exc:`ValueError` is raised.
1315+
1316+
If multiple items are minimal / maximal, the function returns the first one
1317+
encountered. This is consistent with other sort-stability preserving tools
1318+
such as ``(sorted(iterable, key=keyfunc)[0], sorted(iterable, key=keyfunc)[-1])``
1319+
and ``(heapq.nsmallest(1,iterable, key=keyfunc), heapq.nlargest(1,iterable, key=keyfunc))``.
12981320

12991321
.. function:: next(iterator, /)
13001322
next(iterator, default, /)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This creates a new builtin function called `minmax`, which does work similar
2+
to the functions `min` and `max`; however, is more efficient than running
3+
`min` and then `max` and computes the smallest and largest elements of the
4+
iterable in a single pass; rather than 2 passes.

0 commit comments

Comments
 (0)