[Python-Dev] [Python-ideas] minmax() function returning (minimum, maximum) tuple of a sequence

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Mon Oct 11 07:56:47 CEST 2010

On Sun, Oct 10, 2010 at 2:55 PM, Zac Burns <zac256 at gmail.com> wrote:

> This could be generalized and placed into itertools if we create a function
> (say, apply for lack of a better name at the moment) that takes in an
> iterable and creates new iterables that yield each from the original
> (avoiding the need for a list) holding only one in memory. Then you could
> pass the whatever function you wanted to run the iterables over an get the
> result back in a tuple.

Time machine partially beat you to this one. Look at the docs on itertools.tee

    tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n

Can be used like so:

>>> it = iter(range(100))
>>> it1, it2 = itertools.tee(it)
>>> max(it1)
>>> min(it2)

This doesn't quite have the memory characteristics you describe, but
it's about as good as you can expect in a single threaded environment.

More information about the Python-Dev mailing list