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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:52:44+00:00 2026-06-18T07:52:44+00:00

I have a generator that will keep giving numbers that follow a specific formula.

  • 0

I have a generator that will keep giving numbers that follow a specific formula. For sake of argument let’s say this is the function:

# this is not the actual generator, just an example
def Generate():
    i = 0
    while 1:
        yield i
        i+=1       

I then want to get a list of numbers from that generator that are below a certain threshold. I’m trying to figure out a pythonic way of doing this. I don’t want to edit the function definition. I realize you could just use a while loop with your cutoff as the condition, but I’m wondering if there is a better way. I gave this a try, but soon realized why it wouldn’t work.

l = [x for x in Generate() x<10000] # will go on infinitely

So is there a correct way of doing this.

Thanks

  • 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-18T07:52:45+00:00Added an answer on June 18, 2026 at 7:52 am

    An itertools solution to create another iterator:

    from itertools import takewhile
    l = takewhile(lambda x: x < 10000, generate())
    

    Wrap it in list() if you are sure you want a list:

    l = list(takewhile(lambda x: x < 10000, generate()))
    

    Or if you want a list and like inventing wheels:

    l = []
    for x in generate():
        if x < 10000:
            l.append(x)
        else:
            break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website that generates pdf file from CSP. Will this pdf file
I have written a Python script that will generate a series of graphs and
I have a powershell script that will generate an email for users whose password
I have a system that will generate messages sporadically, and I would like to
I have a buffer in class 'bufferClass' that will generate a signal to tell
I have within a TabActivity a Spinner that will be generated dynamically. Just to
I have a generator that generates a series, for example: def triangle_nums(): '''Generates a
I have a form generator that makes all the dbEdits the same width. The
I'm developing a system, and I have build a code generator that emits a
Suppose I have a function that returns a closure: sub generator { my $resource

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.