Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: reversed(zip(...)) not working as intended Date: Sun, 6 Mar 2016 12:51:53 -0600 Lines: 27 Message-ID: References: <56DC7727.1090001@mail.de> <20160306123850.6f78ab7c@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de UFfWy8pwgKP5Pees3QoZDg1O+JlGK7kCOM1u+luI3Ddw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'raises': 0.07; 'subject:not': 0.11; '-tkc': 0.16; '12:38,': 0.16; 'ah,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterator': 0.16; 'list)': 0.16; 'received:10.215': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence.': 0.16; 'wrote:': 0.16; 'integer': 0.18; '(the': 0.22; 'arguments': 0.22; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'sequence': 0.27; 'looks': 0.29; 'chase': 0.29; 'methods.': 0.29; "i'm": 0.30; 'skip:_ 10': 0.32; 'returned': 0.32; 'list': 0.34; 'generic': 0.35; 'protocol': 0.35; "isn't": 0.35; 'supports': 0.35; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'method': 0.37; 'charset:us-ascii': 0.37; 'starting': 0.37; 'or,': 0.38; 'why': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'results': 0.66; 'duplication': 0.84; 'received:10.235': 0.84; 'received:23': 0.84 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1457290493725:1201602235 X-MC-Ingress-Time: 1457290493725 In-Reply-To: <20160306123850.6f78ab7c@bigbox.christie.dr> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com 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:104166 On 2016-03-06 12:38, Tim Chase wrote: > On 2016-03-06 19:29, Sven R. Kunze wrote: > > what's the reason that reversed(zip(...)) raises as a TypeError? > > I'm not sure why reversed() doesn't think that the thing returned by > zip() isn't a sequence. Ah, a little more digging suggests that in 2.x, zip() returned a list which "has a __reversed__() method [and] supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0)." In 3.x, zip() returns a generic iterator which neither has a __reversed__() method nor has __len__() and __getitem__() methods. So it looks like one needs to either results = reversed(list(zip(...))) or, more efficiently (doing it with one less duplication of the list) results = list(zip(...)) results.reverse() -tkc