Skip to main content

minmax(x): min(x), max(x) in one pass

Project description

pyminmax

minmax(iterable, *, key=None)
minmax(iterable, *, default, key=None)
minmax(arg1, arg2, *args, key=None)

With a single iterable argument, return its smallest and largest items as a tuple pair. The default keyword-only argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

With two or more arguments, return the smallest and largest arguments as a tuple pair.

The key argument specifies a one-argument ordering function like that used for list.sort().

If multiple items are minimal or maximal, the function returns the first ones encountered.

It is written in C, adapted straight from CPython's implementation of min(), max().

Installation

pip install pyminmax

Usage

>>> from pyminmax import minmax
>>> minmax([5, 2, 0, 100, -100, 10])
(-100, 100)
>>> minmax((), default=1)
1
>>> minmax(5, 2, 0, 100, -100, 10)
(-100, 100)
>>> minmax(5, 2, 0, 100, -100, 10, key=lambda x: -x)
(100, -100)

Test

Once installed, run the test suite via

python -m unittest pyminmax.tests --verbose

Branches

  • 0.1 branch contains the latest release (0.1.4).
  • main is the development branch.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyminmax-0.1.4.tar.gz (7.3 kB view details)

Uploaded Source

File details

Details for the file pyminmax-0.1.4.tar.gz.

File metadata

  • Download URL: pyminmax-0.1.4.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pyminmax-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6d2f65681f207cc4912f3a1d36276fa6dd3979cf9342ceaffdc9d1cd56c22905
MD5 78e52e94ac0afa4c50b589b916252bc0
BLAKE2b-256 1a945ff444d069af8fd8c4703c1222ddafbaa1059ea878fe285bbccdc01683ba

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page