[Python-3000] have zip() raise exception for sequences of different lengths

Jim Jewett jimjjewett at gmail.com
Thu Aug 31 17:38:59 CEST 2006

On 8/30/06, Barry Warsaw <barry at python.org> wrote:
> On Aug 30, 2006, at 5:57 PM, Guido van Rossum wrote:

> > Perhaps a compromise could be to add a keyword parameter to request
> > such an exception? (We could even add three options: truncate, pad,
> > error, with truncate being the default, and pad being the old map()
> > and filter() behavior.)

> What about a keyword argument called 'filler' which can be an n-sized
> sequence or a callable.

How about a keyword-only argument called finish which is a callable to
deal with the problem?  When any sequence is exhausted, its position
is filled with StopIteration, and then finish(result) is returned.

For example,

    >>> g=zip("abc", (1,2))

The third call to g.next() will return the result of
    finish('c', StopIteration)

def finish_truncate(*args):
    # The default, like today
    raise StopIteration

def finish_error(*args):
    if all(v is StopIteration for v in args):
        raise StopIteration
    raise ValueError("Mismatched sequence length %s" % args)

def finish_padNone(*args):
    if all(v is StopIteration for v in args):
        raise StopIteration
    return tuple((v if v is not StopIteration else None) for v in args)


More information about the Python-3000 mailing list