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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:59+00:00 2026-05-26T12:05:59+00:00

The problem is quite simple, I have a method that returns a list. I

  • 0

The problem is quite simple, I have a method that returns a list.
I want to iterate over every item in this list, and once finished,
call the method to receive a new list, and repeat.

At the moment, my code looks something like:

generator = iter([])
while Condition:
    try:
        item = next(generator)
    except StopIteration:
        generator = iter(list_returining_method())
        item = next(generator)
    ...

However, previously, I was using a nested for loop.

while Condition:
    for item in list_returining_method():
        ...

While my previous attempt looks nicer in some respects, but my current method has some ‘advantages’:

  • if Condition is set to false, the loop ends without having to break out of the for loop.
    • an extension of the above reasons, methods with access to Condition can end the loop without going over all other items in the said list, or implementing a special check in the for loop.
  • The first approach allows skipping items in the loop, should the need arise.
  • There is also one less level of indentation. This is more vanity then anything else, but considering that the actual code was part of a class method, the indent level was already pretty high.

To say the least, im confused as to which is more appropriate. They both seem to have unique advantages and disadvantages, so if anyone knows the most correct and pythonic approach, I’d really appreciate 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-05-26T12:05:59+00:00Added an answer on May 26, 2026 at 12:05 pm

    I’d suggest you use some iterators

    items = itertools.chain.from_iterable( iter(list_returning_method, None) )
    
    for item in items:
        # do something with item
        print item
    
        if not Condition:
           break
    

    iter(callable, sentinel) returns an iterator which produces the result of callable() until it return sentinel.

    itertools.chain.from_iterable returns an iterator that flattens the original iterator, it will produce the values in the lists produced by the first iterator.

    I think that gives you most of the advantages of your method but with a cleaner style.

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

Sidebar

Related Questions

I have a quite simple WCF service method which returns an IQueryable, just for
I Have a quite simple question that I just cant figure out. The method
My problem is quite simple: I have a 400MB file filled with 10,000,000 lines
Today we faced a quite simple problem that were made even simpler by the
I've never learnt JavaScript, but I imagine this is quite a simple problem. Just
I have faced this problem quite often during the last couple of months, during
I have an address object that I want to create an equals method for.
I have just run into quite surprising problem. The case is simple: return all
My problem seems to be quite simple, but it's not working the intuitive way.
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:

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.