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 9180687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:06:55+00:00 2026-06-17T18:06:55+00:00

I have a sequence of generators: (gen_0, gen_1, … gen_n) These generators will create

  • 0

I have a sequence of generators: (gen_0, gen_1, … gen_n)

These generators will create their values lazily but are finite and will have potentially different lengths.

I need to be able to construct another generator that yields the first element of each generator in order, followed by the second and so forth, skipping values from generators that have been exhausted.

I think this problem is analogous to taking the tuple

((1, 4, 7, 10, 13, 16), (2, 5, 8, 11, 14), (3, 6, 9, 12, 15, 17, 18))

and traversing it so that it would yield the numbers from 1 through 18 in order.

I’m working on solving this simple example using (genA, genB, genC) with genA yielding values from (1, 4, 7, 10, 13, 16), genB yielding (2, 5, 8, 11, 14) and genC yielding (3, 6, 9, 12, 15, 17, 18).

To solve the simpler problem with the tuple of tuples the answer is fairly simple if the
elements of the tuple were the same length. If the variable ‘a’ referred to the tuple, you could use

[i for t in zip(*a) for i in t]

Unfortunately the items are not necessarily the same length and the zip trick doesn’t seem to work for generators anyway.

So far my code is horribly ugly and I’m failing to find anything approaching a clean solution. Help?

  • 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-17T18:06:56+00:00Added an answer on June 17, 2026 at 6:06 pm

    You might consider itertools.izip_longest, but in case None is a valid value, that solution will fail. Here is a sample “another generator”, which does exactly what you asked for, and is pretty clean:

    def my_gen(generators):
        while True:
            rez = () 
            for gen in generators:
                try:
                    rez = rez + (gen.next(),)
                except StopIteration:
                    pass
            if rez:
                yield rez
            else:
                break
    
    print [x for x in my_gen((iter(xrange(2)), iter(xrange(3)), iter(xrange(1))))]
    
    [(0, 0, 0), (1, 1), (2,)] #output
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this question , I have an endless sequence using Python generators. But the
I have the following: Id(x => x.ID, m => { m.Column(IDUSER); m.Generator(Generators.Sequence); }); But
I have the following sequence: [as seen now in Toad]: CREATE SEQUENCE LOG_ID_SEQ START
I have a generator that generates a finite sequence. To determine the length of
I have been reading various articles about random numbers and their generators. There are
I have some code: @Id @SequenceGenerator(name = SOMETHING_SEQ) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SOMETHING_SEQ)
I have a sequence which varies in length e.g. something like: items <- 1:4
I have a sequence like [0, 1, 0, 1, 0, 1, 0] and I
I have a sequence of maps. ;; input [{:country MX, :video 12345, :customer cid1}
I have a sequence of k nodes N1 , N2 , N3 , ...

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.