Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6046295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:13:22+00:00 2026-05-23T07:13:22+00:00

I was trying to implement the reverse function of itertools.izip on Python 2.7.1. The

  • 0

I was trying to implement the reverse function of itertools.izip on Python 2.7.1. The thing is that I find a problem, and I don’t have an explantion.
Solution 1, iunzip_v1 works perfectly. But solution 2. iunzip_v2, doesn’t works as expected. Til now, I haven’t found any relevant information about this problem, and reading the PEP about generators, it sound it should work, but it doesn’t.

import itertools
from operator import itemgetter

def iunzip_v1(iterable):
    _tmp, iterable = itertools.tee(iterable, 2)
    iters = itertools.tee(iterable, len(_tmp.next()))
    return tuple(itertools.imap(itemgetter(i), it) for i, it in enumerate(iters))

def iunzip_v2(iterable):
    _tmp, iterable = itertools.tee(iterable, 2)
    iters = itertools.tee(iterable, len(_tmp.next()))
    return tuple((elem[i] for elem in it) for i, it in enumerate(iters))

result:

In [17]: l
Out[17]: [(0, 0, 0), (1, 2, 3), (2, 4, 6), (3, 6, 9), (4, 8, 12)]

In [18]: map(list, iunzip.iunzip_v1(l))
Out[18]: [[0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12]]

In [19]: map(list, iunzip.iunzip_v2(l))
Out[19]: [[0, 3, 6, 9, 12], [0, 3, 6, 9, 12], [0, 3, 6, 9, 12]]

Seems that iunzip_v2 is using the last value, so the generators aren’t keeping the value while they are created inside the first generator.
I’m missing something and I don’t know what is.

Thanks in advance if something can clarify me this situation.

UPDATE:
I’ve found the explanation here PEP-289, my first read was at PEP-255.
The solution I’m trying to implement is a lazy one, so:

  zip(*iter) or izip(*...)

doesn’t work for me, because *arg expand the argument list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:13:23+00:00Added an answer on May 23, 2026 at 7:13 am

    You’re reinventing the wheel in a crazy way. izip is its own inverse:

    >>> list(izip(*izip(range(10), range(10))))
    [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]
    

    But that doesn’t quite answer your question, does it?

    The problem with your nested generators is a scoping problem that happens because the innermost generators don’t get used until the outermost generator has already run:

    def iunzip_v2(iterable):
        _tmp, iterable = itertools.tee(iterable, 2)
        iters = itertools.tee(iterable, len(_tmp.next()))
        return tuple((elem[i] for elem in it) for i, it in enumerate(iters))
    

    Here, you generate three generators, each of which uses the same variable, i. Copies of this variable are not made. Then, tuple exhausts the outermost generator, creating a tuple of generators:

    >>> iunzip_v2((range(3), range(3)))
    (<generator object <genexpr> at 0x1004d4a50>, <generator object <genexpr> at 0x1004d4aa0>, <generator object <genexpr> at 0x1004d4af0>)
    

    At this point, each of these generators will execute elem[i] for each element of it. And since i is now equal to 3 for all three generators, you get the last element each time.

    The reason the first version works is that itemgetter(i) is a closure, with its own scope — so every time it returns a function, it generates a new scope, within which the value of i does not change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to implement something that combines reverse engineering and graph theory. Therefore
Trying to implement a timer for my game that I'm making. I have a
Trying to implement a simple print in SL4. I have a DataGrid that I
when trying to implement an Aspect, that is responsible for catching and logging a
I'm trying to implement an octree, and for that, I need a fast AABB-ray
I am working on an image manipulation problem. I have an overhead projector that
I'm trying to implement an Undo/Redo stack in a C# application that I'm working
Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement the following behavior on an iPad. I have a map-centric application
Trying to implement some nested loops that are spitting out good old nested html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.