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

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.2.tar.gz (6.9 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pyminmax-0.1.2.tar.gz
  • Upload date:
  • Size: 6.9 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.2.tar.gz
Algorithm Hash digest
SHA256 233e55b8b5a8bd1870262abd0bebbed5863413d27c72e150bd699ce18a9a422e
MD5 d6a8fbe3c6904306ea6c85b8e8b1abf3
BLAKE2b-256 5c053d05ec5f83489863f575f8ec99c568aef2b9c2c5e93be185a4363b564144

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