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

MRAB python at mrabarnett.plus.com
Sun Mar 6 14:00:51 EST 2016


On 2016-03-06 18:29, Sven R. Kunze wrote:
> Hi,
>
> what's the reason that reversed(zip(...)) raises as a TypeError?
>
> Would allowing reversed to handle zip and related functions lead to
> strange errors?
>
'reversed' yields the items in reverse order; it needs the last item first.

Iterators yield items from the first to the last.

'reversed' would have to get and store all of the items from the 
iterator. It won't know which is the last item until the iterator raises 
StopIteration.

Only then will it be able to yield the items in reverse order.

It's much better for it to complain, and leave it for the user to do it 
explicitly with reversed(list(zip(...))).




More information about the Python-list mailing list