On Mon, Apr 20, 2020 at 08:42:00PM +0300, Ram Rachum wrote: > Here's something that would have saved me some debugging yesterday: > > >>> zipped = zip(x, y, z, strict=True) > > I suggest that `strict=True` would ensure that all the iterables have been > exhausted, raising an exception otherwise.
Here you go, add this to your personal toolbox: from itertools import zip_longest def zip_strict(*iterables): sentinel = object() for t in zip_longest(*iterables, fillvalue=sentinel): if sentinel in t: p = t.index(sentinel) msg = "argument %d exhausted" raise ValueError(msg % p) yield t I added that to my personal toolbox sometime, oh, eight years or so ago, and have never used it since, so it's brand new :-) > This is useful in cases where you're assuming that the iterables all have > the same lengths. When your assumption is wrong, you currently just get a > shorter result, and it could take you a while to figure out why it's > happening. That assumes that you care that the assumption is wrong, rather than just saying "... if they aren't the same length, truncate at the shortest iterable". Can you give a little more detail on your use-case where the consumer of the data needs to care that all the iterables are the same length? This approach has (at least) one **big** problems as far as I am concerned: - it rules out the use of infinite iterators like itertools.count() > What do you think? I'm tempted to say YAGNI, which would be a pretty brave thing to say given that you just said you did need it :-) -- Steven _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/K7H3HFDG2OMPNNL5ELME5OVOY55CHYVE/ Code of Conduct: http://python.org/psf/codeofconduct/