but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?

That is what xrange(n) is for. It avoids creating a list of numbers, and instead just provides an iterator object.

In Python 3, xrange() was renamed to range() - if you want a list, you have to specifically request it via list(range(n)).

Answer from Amber on Stack Overflow
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ python โ€บ while-loop-in-python
While Loop in Python with Examples - Scaler Topics
June 18, 2023 - Python While Loop is a programming construct used to repeatedly execute a set of statements until a condition is met. When the condition in the program becomes false, the line immediately after the loop is performed.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_loop.asp
Python - Loop Lists
Use the len() function to determine the length of the list, then start at 0 and loop your way through the list items by referring to their indexes. Remember to increase the index by 1 after each iteration. Print all items, using a while loop to go through all the index numbers
Discussions

python - for or while loop to do something n times - Stack Overflow
In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let's have a look on two simple pieces of code: for i in range(n): More on stackoverflow.com
๐ŸŒ stackoverflow.com
How while loops affect other codes running
I need help understanding while loops. I know this should be basic knowledge, but I canโ€™t get a clear picture of the while loop with code outside of the loop. In my case I have to have something done once every 24 hours.โ€ฆ More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
December 21, 2022
Reddit
I've been writing Python scripts for a while now. Nothing crazy, just automating small stuff, scraping some data, making my life a little easier. I thought I had a decent handle on things. I was looking at someone else's code and they used a list comprehension in a way that made me stop and read it three times. I realized I had been writing loops ... More on reddit.com
๐ŸŒ r/learnpython
October 29, 2018
[Python] Why would I use a for loop instead of a while loop?

for means "do something with each of these things".

while means "keep doing this until something is no longer true".

So... say what you mean. Readability counts.

More on reddit.com
๐ŸŒ r/learnprogramming
59
49
November 21, 2011
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.3 documentation
In a for loop, the else clause is executed after the loop finishes its final iteration, that is, if no break occurred. In a while loop, itโ€™s executed after the loopโ€™s condition becomes false.
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ python-while-loop-example
8 Python while Loop Examples for Beginners | LearnPython.com
February 5, 2024 - Letโ€™s go over a simple Python while loop example to understand its structure and functionality: >>> i = 0 >>> while i < 5: >>> print(i) >>> i += 1 ... The condition in this while loop example is that the variable i must be less than 5. The initial value of the variable i is set to 0 before the while loop.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-while-loop
Python While Loop - GeeksforGeeks
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.
Published ย  December 23, 2025
๐ŸŒ
Substack
datascientistdude.substack.com โ€บ p โ€บ worthwhile-words-about-for-vs-while
Worthwhile Words about For vs. While Loops in Python
3 weeks ago - While Loop: More flexible but potentially more error-prone. It is ideal when you want to continue looping until a certain condition is met, especially when the number of iterations is not predetermined. Use when the number of iterations depends on a condition and isnโ€™t fixed in advance. Python also provides loop control statements that can modify the flow of a loop:
๐ŸŒ
Altcademy
altcademy.com โ€บ blog โ€บ what-is-a-while-loop-in-python
What is a while loop in Python
January 31, 2024 - A while loop in Python is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The code block will keep running over and over as long as the condition is true.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied.
Published ย  1 month ago
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.02-While-Loops.html
While Loops โ€” Python Numerical Methods
The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon! ... A while loop or indefinite loop is a set of instructions that is repeated as long as the associated logical expression is true.
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ python while loop
How to use while loops in Python - IONOS
September 26, 2022 - The Python while loop can be used to execute a block of code for as long as a certain condition is fulfilled. While loops are primarily used in Python when the number of iterations canโ€™t be determined at the time of writing the code.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python exercises โ€บ python if else, for loop, and range() exercises with solutions
Python if else, for loop, and range() Exercises with Solutions
April 19, 2025 - Control flow statements: Use the ... loop with range(), we can repeat an action a specific number of times. while loop: To execute a code block repeatedly, as long as the condition is True....
๐ŸŒ
Plain English
python.plainenglish.io โ€บ while-loops-in-python-when-and-how-to-use-them-100ce5800e42
While Loops in Python โ€” When and How to Use Them | Python in Plain English
June 26, 2025 - Thatโ€™s basically how a while loop works in Python. You tell your code: โ€œHey, keep doing this while this condition is true.โ€ And as soon as that condition changes (like your coffee being done), the loop stops.
๐ŸŒ
Alma Better
almabetter.com โ€บ bytes โ€บ tutorials โ€บ python โ€บ while-loop-in-python
While Loop in Python
October 2, 2024 - The Python While Loop ๐Ÿ” repeatedly executes a block of statements until a specified condition :thinking: becomes false. Once the condition becomes false, the first line after the Loop executes.
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
November 12, 2024 - The while loop is a fundamental tool in Python for executing a block of code repeatedly as long as a given condition remains true. This type of loop is useful when the number of iterations isnโ€™t known in advance and is determined by a condition ...
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How while loops affect other codes running - Python Help - Discussions on Python.org
December 21, 2022 - I need help understanding while loops. I know this should be basic knowledge, but I canโ€™t get a clear picture of the while loop with code outside of the loop. In my case I have to have something done once every 24 hours. That is the whole while loop with sleep(1). I have that.
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - Browse Topics Guided Learning Paths ... web-dev web-scraping ... Pythonโ€™s while loop enables you to execute a block of code repeatedly as long as a given condition remains true....
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ all about python while loop with examples
Python While Loop with Examples and Use Cases
January 31, 2024 - Visualizing Patterns and Trends ... ... A while loop is a fundamental control flow statement in Python that allows you to repeatedly execute a block of code as long as a certain condition is true....
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list. ... While loops, like the ForLoop, are used for repeating sections of code - but unlike a for loop, the while loop will not run n times, but until a defined condition is no ...