Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Simple exercise Date: Mon, 14 Mar 2016 17:00:53 +0100 Organization: None Lines: 49 Message-ID: References: <88c5b5fa-66a0-461a-8ae4-b3264b32f679@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de sCkg/2kPFCUdhHtUE7z9bgZ9+/IKpV7hGEhR50ICM3jA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'indices': 0.07; 'reason,': 0.07; '(1,': 0.09; 'benjamin': 0.09; 'default;': 0.09; 'length.': 0.09; 'lengths': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'warn': 0.09; 'exception': 0.13; 'index': 0.13; 'useful,': 0.13; '(2,': 0.16; '(9,': 0.16; '2016': 0.16; 'dropping': 0.16; 'iterables': 0.16; 'iterator': 0.16; 'iterator:': 0.16; 'itertools': 0.16; 'length,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'x).': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'meant': 0.22; 'pass': 0.22; 'am,': 0.23; 'advance.': 0.23; 'import': 0.24; 'mon,': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'rest': 0.26; 'error': 0.27; 'equivalent': 0.27; '14,': 0.27; 'function': 0.28; 'values': 0.28; "i'm": 0.30; 'strongly': 0.30; 'anyone': 0.32; 'run': 0.33; 'problem': 0.33; 'items.': 0.33; 'raised': 0.33; 'raising': 0.33; 'subject:Simple': 0.33; 'though.': 0.33; 'could': 0.35; 'something': 0.35; 'item': 0.35; 'sometimes': 0.35; 'too': 0.36; 'skip:i 20': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'johnson': 0.37; 'or,': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'avoid': 0.61; 'march': 0.64; 'mar': 0.65; 'else.': 0.66; 'matter.': 0.66; 'percent': 0.66; 'hoping': 0.77; 'oscar': 0.84; 'absolutely': 0.88; 'rick': 0.93; 'hundred': 0.96 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8ff1.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104832 Ian Kelly wrote: > On Mon, Mar 14, 2016 at 9:06 AM, Oscar Benjamin > wrote: >> On 14 March 2016 at 14:35, Rick Johnson >> wrote: >>> >>> I would strongly warn anyone against using the zip function >>> unless >> ... >>> I meant to say: absolutely, one hundred percent *SURE*, that >>> both sequences are of the same length, or, absolutely one >>> hundred percent *SURE*, that dropping values is not going to >>> matter. For that reason, i avoid the zip function like the >>> plague. I would much rather get an index error, than let an >>> error pass silently. >> >> I also think it's unfortunate that zip silently discards items. Almost >> always when I use zip I would prefer to see an error when the two >> iterables are not of the same length. > > It's sometimes very useful, though. For example on multiple occasions > I've taken advantage of the fact that enumerate(x) is equivalent to > zip(itertools.count(), x). If zip raised an error then that would only > be possible using islice, and then only if the length is known in > advance. > > Also, in order for zip to know that the lengths are not equal, it > would have to try to read one additional item from the longer > iterable. That's rather unfortunate if it's an iterator and you're > hoping to catch the exception and then use the rest of the iterator > for something else. That's a problem you may run into with the current zip(), too -- unless you know that the first is the shortest iterator: >>> from itertools import count >>> indices = count() >>> for items in "abc", "def", "gh": ... list(zip(indices, items)) ... [(0, 'a'), (1, 'b'), (2, 'c')] [(4, 'd'), (5, 'e'), (6, 'f')] [(8, 'g'), (9, 'h')] I'm with Oscar here, raising an exception would be the better default; the current implementation could have been made available as itertools.zip_shortest().