I find that about 90% of the time I want want to zip iterators together, I
expect them to be the same length and want to throw an exception if they
aren't.  Yet there is not currently a solution for this in the standard
library for this, and as a result I always have to take this function
everywhere I go:


    def zip_equal(*iterables):
        """
        Zip and raise exception if lengths are not equal.

        Taken from solution by Martijn Pieters, here:

http://stackoverflow.com/questions/32954486/zip-iterators-asserting-for-equal-length-in-python

        :param iterables: Iterable objects
        :return: A new iterator outputting tuples where one element comes
from each iterable
        """
        sentinel = object()
        for combo in zip_longest(*iterables, fillvalue=sentinel):
            if any(sentinel is c for c in combo):
                raise ValueError('Iterables have different lengths.
Iterable(s) #{} (of 0..{}) ran out first.'.format([i for i, c in
enumerate(combo) if c is sentinel], len(combo)-1))
            yield combo

Would anybody object to adding this to the standard library for Python 3.8?
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to