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

Uploaded Source

File details

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

File metadata

  • Download URL: pyminmax-0.1.1.tar.gz
  • Upload date:
  • Size: 6.5 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.1.tar.gz
Algorithm Hash digest
SHA256 a2d5990f4056a54e6c883d188acb2163e79e638463080790280c7608a791e0e4
MD5 29eb722703619ac3b5723ca0c294b9e5
BLAKE2b-256 b1941b796312c5ccb9626b6562fd8001dfecbacc342bc58df3695d75fec54dc6

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