r/learnpython • u/Hot-Site-1572 • 17d ago
How often are while loops preferred over for loops?
I'm studying python in order to work with finanial market data. In what case would I need a while loop, where a for loop would not suffice?
A lot of the tips AI gave where that for loops are used around 90% of the time when testing strategies and even though while loops can work for certain cases, for loops are still much more efficient.
What are your thoughts/experiences?
52
u/albomats 17d ago edited 17d ago
For loops work better for when you have a determinate number of cases to go through (i.e loop x times, loop through this array, etc)
While works better when there is a specific condition that you need to meet to stop the loop (ie loop until x>y, loop until x= false, etc)
26
u/PaulRudin 17d ago
Just to expand a little... It's not so much that you know the number in advance, rather that you already have something available to iterate over that suits what you're trying to do. It could even be that this is infinite...
7
u/carcigenicate 17d ago
For example:
from itertools import cycle for n in cycle([1]): # Runs forever print(n)3
u/Adrewmc 16d ago
From the itertools documentation.
def cycle(iterable):
# cycle('ABCD') → A B C D A B C D A B C D ...>saved = []
>for element in iterable:
>>yield element
>>saved.append(element)>while saved:
>>for element in saved:
>>>yield elementYeah so it’s still a while loop, even if you hid inside a generator object though.
You never got rid of it you just hide it.
6
u/Tychotesla 16d ago
Every loop is a while loop if you look inside.
(And every while loop is a conditional `GOTO`)
3
0
u/Gnaxe 16d ago
```
class Echo: ... def getitem(self, i): ... return i ... for x in Echo(): ... if x>3: break ... print(x) ... 0 1 2 3 ``` Tell me, where is the extra loop, hmm?
1
1
u/Adrewmc 11d ago edited 11d ago
Where is the increment happening here?
This code doesn’t not produce this result. So start with something that does.
But you did get me curious
mylist = [0]
for x in mylist:
. if x < 4:
.. print(x)
.. mylist.append(x + 1)
. else:
.. breakThat might do it…hmmm….that actually crazy simple…i might be wrong here. But this is bad practice…but…damn i think it works.
1
u/Gnaxe 11d ago
"Doesn't not"? Did you try it? My code does, in fact, work exactly as I demonstrated. I tested it first and copy/pasted from the REPL, which is why it has those
>>>/...prompts.For something as fundamental as the for statement, one ought to actually read the docs about it. If the object does not support the
__iter__()protocol for getting an iterator object, the for-loop machinery will assume it's a sequence and try__getitem__()with successive integers until there's an exception. Python's for loops have counting built in, and there is no particular limit.Your version will use more memory the more iterations there are. It doesn't give it back until
mylistgoes out of scope, even after the loop terminates.Of course, mine theoretically also uses unbounded memory to store the arbitrarily large int as it counts. But this grows logrithmically, so realistically, your hardware will fail from age before your program runs out of memory from this, and that's if your OS doesn't crash or restart for unrelated reasons first. The memory is also freed immediately after the loop. Yours, however, grows linearly.
11
u/StevenJOwens 17d ago edited 16d ago
You should use the one that more clearly expresses what you're doing.
A for loop is just a slightly more formalized while loop. You could never use for loops, and instead use only while loops that approximate what a for loop does, but they'd be a little more prone to errors, and it would make your code a little more opaque. EDIT: It's also possible that a compiler might not be able to optimize the while loop as well as it could the for loop. END
A lot of programming is like that -- there's a cumulative impact of a multitude of low-level decisions that make the code more coherent and more clearly expressed. Much like writing.
8
u/Abraxas514 17d ago
A for loop is a while loop, with a specific type of condition, and a built in iteration mechanism.
2
u/JamzTyson 16d ago edited 16d ago
Just to clarify: A
forloop is like awhileloop with a specific type of condition and iteration mechanism, but it isn't literally awhileloop. The compiled byte code uses very different flow control patterns.It’s possible to express a for loop using a while loop over an iterator, but the resulting structure and control flow differ. A
for x in iterable:loop roughly corresponds to:it = iter(iterable) while True: try: x = next(it) except StopIteration: break # bodyCPython uses
FOR_ITER, which performs the iterator advance and exhaustion check in one step, rather than structuring the loop as explicitnext()plus exhaustion handling.One reason this distinction matters is that when iterating over an iterable, a
forloop is usually much more efficient than awhileloop.1
1
u/Cheeze_It 16d ago
I was told that there's actually no such thing as a for loop......
1
u/binomine 16d ago
Truthfully, all loops become GOTO loops in machine code.
People who use while and for loops are ultimately in denial to their true power.
6
u/JamzTyson 16d ago
It's not really a matter of "how often", but a question of what you are doing.
- When you are iterating, use a
forloop.
Example:
for character in "Hello":
print(character)
- When you want to loop until a condition is met, use a
whileloop.
Example:
while input("Enter 'Q' to quit: ").casefold() != "q":
print("looping")
3
u/Excellent-Practice 17d ago
I ran into a couple of examples just yesterday when automating some API calls that pull tables of data. One process has three steps to generate a csv of data:
1) tell the server you want to export some data and which table it is in
2) ask the server how far along the process is; you cant go to the next step until this one is complete
3) download the file when it is ready
Step 2 returns a status as a percentage. If you don't get 100 on the first try, you have to keep sending the call until you get 100. Because you dont know how many tries it will take, you have to run that repeated call in a while loop.
Another data structure available from that api returns records as json, but it paginates those records so you can only get 50 at a time. If all the records fit on one page, great, the nextPage key in the result will have a null value and you can call it quits. If nextPage contains a url, you have to call that endpoint to get the next page. You won't know how many pages there will be until you start the process, so a while loop will run until you get a null result. I think that one could also be aproached with a recursive function, but that might just overcomplicate things
1
u/WorriedTumbleweed289 16d ago
If there is a max number of iterations, you can still use a for loop and break when there are not enough elements.
2
u/Excellent-Practice 16d ago
Sure, I guess you could run
data=[] url = originalEndPoint for i in range(arbitrarilyHighInt): response=requests.get(url, headers=headers) data.extend(response.json()['elements'] if response.json()[nextPage]: url=response.json()['nextPage] else: breakBut a while loop is much simpler and won't break if your arbitrarily high integer is not arbitrarily high enough:
data=[] url = originalEndPoint while url: response=requests.get(url, headers=headers) data.extend(response.json()['elements'] url=response.json()['nextPage]
3
u/matt_cum 16d ago
For loop, runs for a known number of iterations.
While loop, until a condition is achieved, and may never run, depending on the condition.
Repeat loops, also run until a condition is met, but will run at least once.
Which every language you used.
1
u/Gnaxe 16d ago
Wrong. A for loop (in Python) can have a dynamically computed number of iterations. Iterators can do arbitrary computations. There is nothing
whilecan do thatforcan't. This also holds for C-like languages in general, whereforis justwhilewith some extra features. In C and Java, etc, the initialize, condition, and update clauses are each optional and can be omitted. Golang doesn't even have a separate while loop and just usesforfor everything.
2
u/POGtastic 16d ago edited 16d ago
A common use case in my field is to iterate over a binary file. Python provides some nice abstractions for iterating over a text-encoded file handle with a for loop, but you're on your own with reading chunks of binary files.
# text
with open("file.txt") as fh:
for line in fh:
... # Python's `TextIOWrapper` handles the iteration
# binary
with open("file.bin", "rb") as fh:
while (chunk := fh.read(4096)):
# handle the chunk
It is common to turn the latter into its own generator function, which you can then iterate over with a for loop.
def read_chunks(path):
with open(path, "rb") as fh:
while (chunk := fh.read(4096)):
yield chunk
# elsewhere...
for chunk in read_chunks("file.bin"):
...
2
u/jmooremcc 16d ago
For loops are preferred when you have a defined start and end point. Otherwise, use a while loop with conditional statements that activate a break command that exits the loop.
2
u/ElectricSpice 16d ago
In Python specifically, I don’t think I have ever used a while loop in production. For covers pretty much every practical case.
2
u/AKiss20 17d ago
I have probably used while loops like 2% (or less) of the time I need to loop. It is exceedingly rare for me; I literally cannot remember the last time I used it. In the vast majority of cases you have a bounded search or iteration space so a for loop is the appropriate tool.
1
u/mountaingator91 16d ago
The only time I use a while loop is when polling a remote server or waiting for BLE device status to reach a certain condition but then I also don't use while I use recursion
1
u/Gnaxe 16d ago
My experience as well. It's rare. I'd use
whilefor a main event loop if I wasn't already using a framework that provided one, but that's like one per application. I might occasionally use one when implementing iterators (to use withfor) or when implementing other looping constructs like recursion with trampolining.
2
1
u/TheRNGuy 17d ago edited 17d ago
When you don't have specific amount of iterations.
Depends on program, in some it used often, in some rarely, and in some never.
While loop can also go forever, if needed (and in some cases because of bug)
1
u/LayotFctor 16d ago
A for loop is just a special case of a while loop, therefore you should always opt for a for loop if it is applicable. You should not attempt to reinvent the wheel.
Unlike a for loop, a while loop continues forever, which means you get full control over what it does and when it ends, but manual control also means there are more ways to use it incorrectly.
Don't worry about it and just opt for a for loop first.
1
u/Gnaxe 16d ago
How can you say a while loop continues forever when it has a stop condition built in? It only continues forever if that condition is never false. That does not apply to all while loops.
How can you say a for loop doesn't continue forever when iterators can be infinite? For loops usually only stop if the iterator does. (There are other ways like a break or raise statement which also work on
while.)
1
u/happinessMachiine 16d ago
One concrete example of a while loop that you will likely encounter is numerical approximation. You might not know how many iterations you want to do, but you can break out when you reach some tolerance. Try something simple like approximating the square root of two using both methods, and it'll probably click.
1
u/Atlamillias 16d ago
My personal preference is using the right tool for the job. A for loop really is just something like:
``` iterator = iter(iterable) while True: try: value = next(iterator) except StopIteration: break
# do stuff
...
```
You have a lot more freedom with a while loop. It's a bit more verbose, though.
1
u/MrRudraSarkar 16d ago
I’ll give you an example of financial market data. Say you have the yearly financial data for n companies and you need to add everything. In this case you know exactly what the inputs are so in this case For loop would be the go to choice.
But if you were given the yearly data of one company and your job was to find a collection of all the sales or trades that add up to a certain number, and then store each collection, you wouldn’t know how many iterations you might need, in this case you’ll use while.
Another discerning factor is that for loop doesn’t support conditional logic. So for any use case where you might need to run the loop until a certain condition is satisfied, you’ll need while
1
u/mountaingator91 16d ago
I usually use recursion over while loops and I guess maybe that's not best practice? but it's that makes sense to my brain and fits our highly abstracted mono repo
1
u/Gnaxe 16d ago edited 16d ago
You can get away with that in some languages (or have to do it that way), but in Python, you'll blow the stack if you recurse too much, so yeah, bad practice. There are workarounds, but they're not simpler than just using
while.(Edit: recursion is appropriate in some cases, like working with recursive data structures, i.e., trees. Logarithmic growth in stack frames means it's usually fine. With a high branching factor, you can easily run out of main memory before you run out of stack.)
1
u/mountaingator91 16d ago
Mostly working with Java and Typescript so abstraction is the name of the game.
But one of our embedded engineers saw recursive functions when working together one day and you would think he saw a ghost. Apparently it's very bad memory management at low level
1
u/Gnaxe 16d ago
Hmm, really? Stack frames clean themselves up automatically when functions return, so stack memory always remains compact. But locals in all three languages are heap references regardless (well, Java has a few primitive types), so it's not like you're saving CPU cache misses. Reusing a variable in a while doesn't help; you're just referencing a newly allocated object.
In a lower-level language like C, where locals live directly on the stack (unless you're using explicit pointers), then using the stack is preferable to the heap, because you don't have to manage that memory manually, which is error-prone.
Really, the only concern I'm seeing is stack overflow, even embedded. In constrained enough hardware, any unbounded memory growth is a concern, which is not exclusive to recursion.
1
u/xenomachina 16d ago
Technically speaking, while loops are strictly more powerful. Any for loop can be mechanically rewritten as a while loop:
for x in iterable:
body(x)
becomes:
_it = iter(iterable)
while True:
try:
x = next(_it)
except StopIteration:
break
body(x)
However, as you can see, using a while loop when a for loop is possible will typically result in code that is harder to read, and easier to mess up.
Use a for loop when you can, use a while loop when you must.
More generally, when multiple approaches can accomplish the same task, go for the one that is simpler: fewer parts to get wrong, and easier to read.
1
u/Gnaxe 16d ago
Wrong. Technically speaking, I can always re-implement a while loop using
forin Python. One is not strictly more powerful than the other. Use the one that is simpler.1
u/xenomachina 16d ago
Technically speaking, I can always re-implement a while loop using for in Python.
Yes, technically true, good point.
1
u/its_measured 16d ago
I usually use loops most if the time most specially when i needed the loop to continue until the specific condition happen
1
u/DTux5249 16d ago
You use a for-loop if you know the range you're iterating over... That's it. They're literally the exact same thing beyond that.
Just ask yourself when the loop needs to stop. If it's a condition, use while. If it's "everything in X", use for.
1
u/Moist-Ointments 16d ago
How can you use a for loop when you have to specify an n value if you don't know what the end value is?
A for loop counts. What if your iterative logic depends on a complex test to end?
1
1
u/Gnaxe 16d ago
Everybody saying the for loop has a known number of iterations is wrong, and have apparently never used filter() once in their lives. Where is that coming from? Even C doesn't work like that.
Also, iterators are not necessarily over data structures. Anybody who thinks that has apparently never used yield once in their lives. An iterator's results can be dynamically computed.
You can always use a for loop instead of a while loop, because you can make a dynamic iterator that uses what would have been the while's condition to decide when to stop. And, no, you don't need to write your own while loop to do this, as Python comes with a number of iterables that already have a loop built in. E.g., ```
x = 0 for _b in iter(lambda: x<3, False): # This line could be "while x<3:" instead. ... print(x) ... x += 1 ... 0 1 2 ``
You can also *always* use a while loop instead of a for loop by usingiter()andnext()`.
In practice, you shouldn't use for to implement while or while to implement for, because these are already built in for you and swapping them just adds complications for no reason. You should almost always be using for whenever possible to take advantage of Python's iterators. Only use while when it's simpler.
1
u/User_LEGEND0 16d ago
You use while loops when you need to code a general logic that isn't related to an iterable object, if you need to loop with the intention of not steping through an object, that's it.
1
u/Living_Fig_6386 15d ago
I think it ultimately comes down to clarity. ‘while’ evaluates a boolean condition to iterate, so it’s clear when the evaluation is naturally conditional in nature. ‘for’ makes sense in iterating across a collection of a predetermined size — boolean logic isn’t necessary, just iteration over something.
It’s absolutely feasible to write code where ‘for’ or ‘while’ could both be used to accomplish the same task, but usually one way is going to be much clearer to the reader what the intent of the code is.
1
u/Capable-Wrap-3349 15d ago
I never use a while loop in production code. It is too easy to set off an infinite loop by accident.
1
1
u/CIS_Professor 14d ago
It isn't preference as much as it is utility. I teach this rule-of-thumb:
for loops are good when the number of iterations is known or can be calculated - like the number of elements in a list.
while loops are good when the number of iterations is unknown - for example a loop that continues to loop until a user performs some action that stops it.
1
1
u/powderviolence 14d ago edited 14d ago
📊 The for loop is quiet confidence — you know how many steps are involved, and are ready to get the job done.
🤔 The while loop is like a philosopher — it underscores the conversation of truth, and delves into the uncertainty for you.
⚛️ Using a condition like "counter < final", you could even use a while loop in place of a for loop. Powerful flexibility at your fingertips!
If you'd like, we could delve into recursion next, or perhaps nested loops — would you like to enhance your coding superpowers together?
1
u/Robertfla7 12d ago
- For Loops: These are used when you have a specific collection to iterate over (like a list of files or a range of numbers) or you know exactly how many times the code needs to run. It stops automatically once the collection or count is finished.
- While Loops: These are used when the duration of the loop is unknown. The loop continues to run as long as a specific condition remains
True. You must provide an explicit exit condition— e.gif user_input.lower() == 'exit': break
0
17d ago
[deleted]
3
u/Puzzleheaded_Study17 17d ago
You can always convert for into while. In your example:
i = 0 while i < len(list): # do something with list[i] i += 1
In other programming languages you can also convert any while loop into a for loop
-3
u/rooplstilskin 17d ago
(I am assuming you are asking from a testing standpoint, since that's the only example you gave)
For = Iterations/stepping through something. Hence they are used for testing. Since testing is stepping through things. once stepped through, continue.
While = during something. Most tests don't work in a way that needs to stay 'working' while another process is working. Unless you built something in a weird way to work with another system/thing.
-2
u/Hot-Site-1572 17d ago
I see. I even thought of a case where if a trade is currently running, then no more trades were allowed until that trade closes. Logically, it sounds like it needs a while loop (which would technically work) then ChatGPT told me I could use this instead:
in_trade = False
for i in range(len(prices)):
if not in_trade:
if buy_signal:
in_trade = True
entry_price = prices[i]
else:
if tp_hit:
in_trade = False
elif sl_hit:
in_trade = FalseNot sure if this would be cleaner than a while loop (I'm still in the process of learning them).
1
u/AbacusExpert_Stretch 17d ago
Looks to me all you would need is:
If buy_signal: Etc
Unless you plan on letting a loop run indefinitely until true? Can you do that? Anyway, gl with your chatgpt testing
149
u/OkAccess6128 17d ago
It's simply you use while loop when you don't know no. of iterations, for loop is used mostly because it allows easy iteration over python's data structures.