Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104166 > unrolled thread

Re: reversed(zip(...)) not working as intended

Started byTim Chase <python.list@tim.thechases.com>
First post2016-03-06 12:51 -0600
Last post2016-03-06 12:51 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: reversed(zip(...)) not working as intended Tim Chase <python.list@tim.thechases.com> - 2016-03-06 12:51 -0600

#104166 — Re: reversed(zip(...)) not working as intended

FromTim Chase <python.list@tim.thechases.com>
Date2016-03-06 12:51 -0600
SubjectRe: reversed(zip(...)) not working as intended
Message-ID<mailman.264.1457290507.20602.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web