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 Overflow
🌐
GeeksforGeeks
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.
Discussions

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
🌐 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
🌐 r/learnprogramming
15
1
March 11, 2025
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
🌐 discuss.python.org
3
0
August 31, 2022
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
🌐 r/learnpython
11
52
February 18, 2019
🌐
Real Python
realpython.com › nested-loops-python
Nested Loops in Python – Real Python
February 22, 2025 - Learn how to use nested loops in Python to iterate over multiple sequences and perform repeated actions efficiently in your programs.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-3-nested-loops
5.3 Nested loops - Introduction to Python Programming | OpenStax
March 13, 2024 - A nested loop has one or more loops within the body of another loop. The two loops are referred to as outer loop and inner loop. The outer loop controls the number of the inner loop's full execution.
🌐
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.
Find elsewhere
🌐
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)....
🌐
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:
🌐
Python.org
discuss.python.org › python help
Python Code for nested loops - Python Help - Discussions on Python.org
August 31, 2022 - 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
🌐
Codingem
codingem.com › home › nested loops in python: a complete guide
Nested Loops in Python: A Complete Guide - codingem.com
January 25, 2024 - A nested loop in Python is a loop inside a loop. This guide teaches you how to work with nested loops in Python with illustrative examples.
🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
Nested Loops in Python: Definition & Examples - Lesson | Study.com
July 12, 2019 - The most basic thing we learned ... several times. We also learned that nesting a loop means simply having a loop that has inside its commands another loop....
🌐
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.
🌐
Medium
martinxpn.medium.com › nested-loops-in-python-21-100-days-of-python-8817d1ec028f
Nested loops in Python (21/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - Nested loops in Python (21/100 Days of Python) Nested loops in Python allow you to loop through multiple sequences at the same time. They are useful for iterating through multi-dimensional data …
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › nested for loops in python
Nested For Loops in Python - Spark By {Examples}
May 31, 2024 - The inner loop will execute n number of times for each outer loop iteration. ... Using nested for loops you can iterate over the multi-dimensional data structure, solve any star/number-related patterns, and get mathematical tables/prime numbers of the specified range.
🌐
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 ...