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

  • SEARCH
  • Home
  • 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 8020695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:45:34+00:00 2026-06-04T21:45:34+00:00

If I create two lists and zip them a=[1,2,3] b=[7,8,9] z=zip(a,b) Then I typecast

  • 0

If I create two lists and zip them

a=[1,2,3]
b=[7,8,9]
z=zip(a,b)

Then I typecast z into two lists

l1=list(z)
l2=list(z)

Then the contents of l1 turn out to be fine [(1,7),(2,8),(3,9)], but the contents of l2 is just [].

I guess this is the general behavior of python with regards to iterables. But as a novice programmer migrating from the C family, this doesn’t make sense to me. Why does it behave in such a way? And is there a way to get past this problem?

I mean, yeah in this particular example, I can just copy l1 into l2, but in general is there a way to ‘reset’ whatever Python uses to iterate ‘z’ after I iterate it once?

  • 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-06-04T21:45:35+00:00Added an answer on June 4, 2026 at 9:45 pm

    There’s no way to “reset” a generator. However, you can use itertools.tee to “copy” an iterator.

    >>> z = zip(a, b)
    >>> zip1, zip2 = itertools.tee(z)
    >>> list(zip1)
    [(1, 7), (2, 8), (3, 9)]
    >>> list(zip2)
    [(1, 7), (2, 8), (3, 9)]
    

    This involves caching values, so it only makes sense if you’re iterating through both iterables at about the same rate. (In other words, don’t use it the way I have here!)

    Another approach is to pass around the generator function, and call it whenever you want to iterate it.

    def gen(x):
        for i in range(x):
            yield i ** 2
    
    def make_two_lists(gen):
        return list(gen()), list(gen())
    

    But now you have to bind the arguments to the generator function when you pass it. You can use lambda for that, but a lot of people find lambda ugly. (Not me though! YMMV.)

    >>> make_two_lists(lambda: gen(10))
    ([0, 1, 4, 9, 16, 25, 36, 49, 64, 81], [0, 1, 4, 9, 16, 25, 36, 49, 64, 81])
    

    I hope it goes without saying that under most circumstances, it’s better just to make a list and copy it.

    Also, as a more general way of explaining this behavior, consider this. The point of a generator is to produce a series of values, while maintaining some state between iterations. Now, at times, instead of simply iterating over a generator, you might want to do something like this:

    z = zip(a, b)
    while some_condition():
        fst = next(z, None)
        snd = next(z, None)
        do_some_things(fst, snd)
        if fst is None and snd is None:
            do_some_other_things()
    

    Let’s say this loop may or may not exhaust z. Now we have a generator in an indeterminate state! So it’s important, at this point, that the behavior of a generator is restrained in a well-defined way. Although we don’t know where the generator is in its output, we know that a) all subsequent accesses will produce later values in the series, and b) once it’s “empty”, we’ve gotten all the items in the series exactly once. The more ability we have to manipulate the state of z, the harder it is to reason about it, so it’s best that we avoid situations that break those two promises.

    Of course, as Joel Cornett points out below, it is possible to write a generator that accepts messages via the send method; and it would be possible to write a generator that could be reset using send. But note that in that case, all we can do is send a message. We can’t directly manipulate the generator’s state, and so all changes to the state of the generator are well-defined (by the generator itself — assuming it was written correctly!). send is really for implementing coroutines, so I wouldn’t use it for this purpose. Everyday generators almost never do anything with values sent to them — I think for the very reasons I give above.

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

Sidebar

Related Questions

I need to create one list based on two other lists. But seems like
How can I compare the items in two lists and create a new list
I wonder if it's possible to create two consecutive lists in markdown without them
I need to create two labels and select lists: Worker: |______________| Subcontractor: |______________| But
I'm not great with CSS, but I want to create two select lists with
I am trying to create two select lists both filled with the same times.
I'm trying to create a simple, custom, gallery. I have two lists, one with
What is the best way to create two dynamic unordered lists in Drupal 6.x
I have a struct that contains two lists: struct MonthData { public List<DataRow> Frontline;
Say I create two sets of tuples like so: Dim losSPResults As List(Of spGetDataResults)

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.