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


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

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

Started byPeter Otten <__peter__@web.de>
First post2016-03-06 19:59 +0100
Last post2016-03-06 19:59 +0100
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 Peter Otten <__peter__@web.de> - 2016-03-06 19:59 +0100

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

FromPeter Otten <__peter__@web.de>
Date2016-03-06 19:59 +0100
SubjectRe: reversed(zip(...)) not working as intended
Message-ID<mailman.265.1457290795.20602.python-list@python.org>
Tim Chase wrote:

> On 2016-03-06 19:29, Sven R. Kunze wrote:
>> what's the reason that reversed(zip(...)) raises as a TypeError?
>> 
>> Would allowing reversed to handle zip and related functions lead to
>> strange errors?
> 
> Peculiar, as this works in 2.x but falls over in 3.x:
> 
> $ python
> Python 2.7.9 (default, Mar  1 2015, 12:57:24)
> [GCC 4.9.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> list(reversed(zip(range(10), range(20,100))))
> [(9, 29), (8, 28), (7, 27), (6, 26), (5, 25), (4, 24), (3, 23), (2,
> 22), (1, 21), (0, 20)]
> 
> $ python3
> Python 3.4.2 (default, Oct  8 2014, 10:45:20)
> [GCC 4.9.1] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> list(reversed(zip(range(10), range(20,100))))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: argument to reversed() must be a sequence
> 
> 
> I'm not sure why reversed() doesn't think that the thing returned by
> zip() isn't a sequence.

Because it isn't ;)

[Python 3]

>>> zip("abc")
<zip object at 0x7f6186d27e48>

>>> import collections
>>> isinstance(z, collections.Sequence)
False
>>> isinstance(z, collections.Iterator)
True

zip() in Python 3 is what itertools.izip() used to be in Python 2.

[toc] | [standalone]


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


csiph-web