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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:40:40+00:00 2026-06-09T08:40:40+00:00

Suppose I have a for loop that iterates over a generator and in this

  • 0

Suppose I have a for loop that iterates over a generator and in this loop i’m building a list for later use:

q = []
for index, val in enumerate(['a', 'b', 'c']):
    q.append(index+val)

I want to hold onto the generator without creating a function such as this:

def foo(gen):
    for index, val in gen:
        yield index+val

is it at all possible or is there some inherent problem I don’t see here?
I suppose it should look something like this:

iter_q = something()
for index, val in enumerate(['a', 'b', 'c']):
    q.add_iteration(index+val)

OK, now that I’ve written this, it does seem quite impossible (or useless), since this for loop will have to iterate through the whole list before the “generator” is ready, which makes it just an iterator over a premade list, and not a generator in the useful sense of the object.

Posting anyway because I couldn’t find such a question myself. (plus, maybe someone still has something interesting to say about it)

  • 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-09T08:40:42+00:00Added an answer on June 9, 2026 at 8:40 am

    If I’m understanding your question correctly, I think you might be wanting a generator expression:

    q = (index + val for index, val in enumerate(someOtherGenerator))
    

    That statement creates a new generator q, which will work just like the generator returned by your foo function. You don’t need to assign it to a variable either, you can create generator expressions just about anywhere, such as in a function call:

    doSomethingWithAGenerator(i+v for i,v in enumerate(someOtherGenerator))
    

    There are some kinds of generators that can’t be made in generator expressions, but the most common kinds can be.

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

Sidebar

Related Questions

Suppose I have some code that looks like this: foreach(type x in list y)
I just got a question that I can't answer. Suppose you have this loop
Suppose we have a loop that runs for 100 times. Does using unsigned char
I have a foreach loop that is suppose to check to see if a
Suppose I have a list of items that are currently processed in a normal
Suppose I have a Master that keeps a list of SlaveThread objects. On each
Suppose I have this code executed by two different threads T1 and T2 that
Suppose I have a function in a single threaded program that looks like this
So I have a for loop in my View that is supposed to render
Suppose you have the following function foo . When I'm running a for loop,

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.