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.3.post1.tar.gz (7.0 kB view details)

Uploaded Source

File details

Details for the file pyminmax-0.1.3.post1.tar.gz.

File metadata

  • Download URL: pyminmax-0.1.3.post1.tar.gz
  • Upload date:
  • Size: 7.0 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.3.post1.tar.gz
Algorithm Hash digest
SHA256 d6240d2fcddebf52147798d77f84ef7d0ac88167ad4c9d16f5787b0fce2a2abd
MD5 1a4da4ea810372146f0436a82b45ebbe
BLAKE2b-256 f8f287c236ca4e247efbb073d647db5a8d22df2f54520a8a3741d5452b1ea835

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