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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:38:37+00:00 2026-06-04T03:38:37+00:00

It is common to express for loops as list comprehensions: mylist=[] for i in

  • 0

It is common to express for loops as list comprehensions:

mylist=[]
for i in range(30):
    mylist.append(i**2)

This is equivalent to:

mylist = [i**2 for i in range(30)]

Is there any sort of mechanism by which this sort of iteration could be done with a while loop?

mylist=[]
i=0
while i<30:
    mylist.append(i**2)
    i+=1

Of course with this simple example it’s easy to translate to a for loop and then to a list comprehension, but what if it isn’t quite so easy?

e.g.

mylist = [i**2 while i=0;i<30;i++ ]

(Of course the above pseudo-code isn’t legitimate python) (itertools comes to mind for this sort of thing, but I don’t know that module terribly well.)

EDIT

An (very simple) example where I think a while comprehension would be useful would be:

dt=0.05
t=0
mytimes=[]
while t<maxtime:
   mytimes.append(t)
   t+=dt

This could translate to:

dt=0.05
t=0
nsteps=maxtime/dt
mytimes=[]
for t in (i*dt for i in xrange(nsteps)):
    mytimes.append(t)

which can be written as a (compound) list comprehension:

nsteps=maxtime/dt
mytimes=[t for t in (i*dt for i in xrange(nsteps)] 

But, I would argue that the while loop is MUCH easier to read (and not have index errors) Also, what if your object (dt) supports ‘+’ but not ‘*’? More complicated examples could happen if maxtime somehow changes for each iteration of the loop…

  • 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-04T03:38:38+00:00Added an answer on June 4, 2026 at 3:38 am

    If your while loop justs checks a local variable that is being incremented, you should convert it to a for loop or the equivalent list comprehension.

    You should only use a while loop only if you can not express the loop as iterating over something. An example of a typical use case are checks for the state of an Event, or a low-level loop that calls into native code. It follows that (correctly used) while loops are rare, and best just written out. A while comprehension would just make them harder to read.

    If you just want to return multiple values, you should consider writing a generator.

    For example, your edited algorithm should be written as (using numpy.arange):

    mytimes = numpy.arange(0, maxtime, 0.05)
    

    Alternatively, with a generator:

    def calcTimes(maxtime):
      dt = 0.05
      t = 0
      while t < maxtime:
       yield t
       t += dt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a common way to express the usage of arguments in C++? I
A common issue I have is getting confused what $(this) is referring to. I
A common piece of code I use for simple string splitting looks like this:
Common image questions in regards to developing WP7 applications which are all related to
I'm writing a bunch of web services which all share some common patterns. For
I thought I could just throw this out there and just ask: I have
In my NodeJS express application I have app.js that has a few common routes.
I am not clear on how to post this question, but to express in
When I enable common control visual style support (InitCommonControls()) and I am using any
I'm trying to execute a query which contains batched commands, and supposedly this wasn't

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.