If you're frequently iterating over a Cartesian product like in your example, you might want to investigate Python 2.6's itertools.product -- or write your own if you're in an earlier Python.
from itertools import product
for y, x in product(range(3), repeat=2):
do_something()
for y1, x1 in product(range(3), repeat=2):
do_something_else()
Answer from alicederyn on Stack OverflowGeeksforGeeks
geeksforgeeks.org › python › python-nested-loops
Python Nested Loops - GeeksforGeeks
Explanation: Outer loop runs from 2 to 3. Inner loop runs from 1 to 10. Each inner loop iteration multiplies the outer loop value with the inner loop value. Example 3: Printing using different inner and outer nested loops
Published March 13, 2026
W3Schools
w3schools.com › python › gloss_python_for_nested.asp
Python Nested Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... A nested loop is a loop inside a loop.
In python is there an easier way to write 6 nested for loops? - Stack Overflow
This problem has been getting at me for a while now. Is there an easier way to write nested for loops in python? For example if my code went something like this: for y in range(3): for x in ... More on stackoverflow.com
[Python] Can someone please explain to me how nested for loops work using this example. I don't understand how it works
What does this do? for j in range(i,userInput): print('*', end=' ') If you don't know, try it. Assign values to i and userInput manually and see if you can figure it out. More on reddit.com
Python Code for nested loops
Does anyone know how to print the following pattern using nested loops? 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 More on discuss.python.org
Nested loops
Actually, that does not print 1 to 10, it will print 1 to 9. Perhaps adding information to show which loop iteration you're on would make more sense: >>> for i in range(3): ... for k in range(3): ... print(i, k) ... (0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) (2, 0) (2, 1) (2, 2) So, we have the i-loop with three values: 0, 1, 2. We start with the i equal to zero. Our next instruction is another loop, the k-loop, with three values: 0, 1, 2. So, we print out i and k for each iteration through the k-loop sequence, with i still zero. After we complete the k-loop, there are no more instructions in the i-loop, so we go to the next value in the i-sequence: 1. And, we do that again and again, until we're done with the i-loop values. More on reddit.com
Videos
Tutorialspoint
tutorialspoint.com › python › python_nested_loops.htm
Python - Nested Loops
In Python, when you write one or more loops within a loop statement that is known as a nested loop. The main loop is considered as outer loop and loop(s) inside the outer loop are known as inner loops.
PYnative
pynative.com › home › python › nested loops in python
Python Nested Loops [With Examples] – PYnative
September 2, 2021 - If the outer number and the inner loop’s current number are the same, then move to the next iteration of an inner loop. ... first = [2, 4, 6] second = [2, 4, 6] for i in first: for j in second: if i == j: continue print(i, '*', j, '= ', i * j)Code language: Python (python) Run ... As you can see in the output, no same numbers multiplying to each other. For example, if you had two lists and want to get all combinations of them, To achieve this, you need to use two nested loops as mentioned below.
Top answer 1 of 11
60
If you're frequently iterating over a Cartesian product like in your example, you might want to investigate Python 2.6's itertools.product -- or write your own if you're in an earlier Python.
from itertools import product
for y, x in product(range(3), repeat=2):
do_something()
for y1, x1 in product(range(3), repeat=2):
do_something_else()
2 of 11
15
This is fairly common when looping over multidimensional spaces. My solution is:
xy_grid = [(x, y) for x in range(3) for y in range(3)]
for x, y in xy_grid:
# do something
for x1, y1 in xy_grid:
# do something else
Softuni
python-book.softuni.org › chapter-06-nested-loops.html
Chapter 6.1. Nested Loops - Programming Basics with Python
Test your solution here: ... a rectangle made out of N x N asterisks. ... A nested loop is a construction where the body of one loop (the outer one) stays inside another loop (the inner one)....
Reddit
reddit.com › r/learnprogramming › [python] can someone please explain to me how nested for loops work using this example. i don't understand how it works
r/learnprogramming on Reddit: [Python] Can someone please explain to me how nested for loops work using this example. I don't understand how it works
March 11, 2025 -
userInput = int(input())
for i in range(userInput):
for j in range(i,userInput):
print('*', end=' ')
print()input is: 3
the output is:
* * * * * * I have been looking at videos and different websites, but i cannot understand it.
Top answer 1 of 5
10
What does this do? for j in range(i,userInput): print('*', end=' ') If you don't know, try it. Assign values to i and userInput manually and see if you can figure it out.
2 of 5
5
Break it down by how the Python interpreter would look at it. The user enters a number. Let's say 3. The first for loop resolves to "for i in range(3)". So for the first loop, i = 0. The second loop then resolves to "for j in range(0, 3)." Because it uses the variable i from the parent loop. That prints "* "three times, because it takes three loops to satisfy range(0, 3) When it completes that, it goes back to the first loop and increments i by 1. The next time the nested loop runs, it's now "for j in range(1, 3)" which will only loop two times. Because the user input is range(3), the first loop will only execute three times before exiting.
InterServer
interserver.net › home › python › everything you need to know about python nested loops
Everything You Need to Know About Python Nested Loops - Interserver Tips
August 4, 2025 - In short, whenever you’re dealing with layers or groups of data, nested loops make your job easier and more organized. A for loop is used to repeat actions a fixed number of times. It goes through each item in a list, string, or range. ... Here, range(3) gives numbers from 0 to 2, and the loop prints each one. A while loop runs as long as a condition is true. It’s useful when you don’t know how many times you’ll need to loop. ... The loop stops when i becomes 3. Python has some special keywords to control loops:
WsCube Tech
wscubetech.com › resources › python › nested-loops
Nested Loops in Python: Uses, Working, Syntax, Examples
November 5, 2025 - Understand how Python nested loops work, their syntax, and practical examples to enhance your programming skills and solve complex problems efficiently.
CodeSignal
codesignal.com › learn › courses › exploring-iterations-and-loops-in-python › lessons › exploring-nested-loops-in-python
Exploring Nested Loops in Python
In this code snippet, we have a for loop that iterates over all countries, and within that loop, there is another for loop that cycles through all the sights for the current country. There you have it: a nested loop!
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Python programming language allows to use one loop inside another loop which is called nested loop.
Published 4 days ago
Programiz PRO
programiz.pro › resources › python-nested-loop
Understanding Nested Loops in Python with Logic Building
A nested loop is a loop inside another loop. The outer loop determines how many times the inner loop will execute. This is particularly useful for tasks involving multi-row or multi-column structures, such as grids or patterns.
Drbeane
drbeane.github.io › python › pages › control › nested_loops.html
Nested Loops — Python for Data Science
For some complex tasks, it is necessary to include loops inside of other loops. This is called a nested loop. The simplest case of a nested loops is when we have one loop that itself contains another loop. In this case, we call the first loop that Python encounters the outer loop, and the loop ...