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
In Python, there are two types of loops: for loop and while loop. Using these loops, we can create nested loops, which means loops inside a loop.
Published   1 week ago
🌐
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.
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
can't understand nested for loops 😭 (need help!)
Nested loops can be a headache, but here's how I think about them: the outer loop controls rows, the inner loop handles what's in each row. For patterns like these, start by figuring out how many rows you need and what changes between them. Then, focus on one row at a time - what number are you printing, and how many times? The outer loop usually sets up the number, while the inner loop prints it. Don't sweat it if it doesn't click right away. Start with simpler patterns and work your way up. Play around with the loops, changing values to see what happens. It'll start making sense with practice. More on reddit.com
🌐 r/learnprogramming
14
13
March 20, 2024
Nested loops
Please read the FAQ to see how to format code so the indentation is maintained. This is probably your code for the second part: numbers = [5, 2, 5, 2, 2] for x_count in numbers: output = '' for count in range(x_count): output += 'x' print(output) That will print each line with the number of xs the same as each number in the list. The list is [5, 2, 5, 2, 2] so you should see lines with 5, 2, 5, 2 and 2 xs, and you do. But you want to print lines that start with one x and increase up to the number one less than the number from the list. Look at your first output example that starts: 0 1 2 3 4 0 ... Instead you want: x xx xxx xxxx ... So instead of 3, for example, you want to print xxx, and instead of 2 you want xx. So instead of printing a number, you want that number of x characters. So replace the line print(count) with something that prints the count number of xs. The hint here is that you can multiply a string by an integer. Try running this bit of code: count = 3 print("x" * count) More on reddit.com
🌐 r/learnpython
3
1
November 13, 2022
[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
🌐
W3Schools
w3schools.com › python › gloss_python_for_nested.asp
Python Nested Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A nested loop is a loop inside a loop.
🌐
YouTube
youtube.com › watch
Nested Loops | Programming Fundamentals with Python - YouTube
All JomaClass videos from 2020 are now free to watch. If you enjoy please consider donating here: https://support.joma.io/ This video was uploaded in 2020 in...
Published   October 5, 2025
🌐
Reddit
reddit.com › r/learnprogramming › can't understand nested for loops 😭 (need help!)
r/learnprogramming on Reddit: can't understand nested for loops 😭 (need help!)
March 20, 2024 -

We have a practical exam coming up on my uni regarding nested for loop in which I struggle to. Can you help me understand how does it work. I know that it is a loop within a loop but I can't logically decide what values to put in the inner loop (the range and the increment). I can't wrap my head around it. 😭

My professor typically give us exercises like these:

Exercise 1:
9
7 7 7
5 5 5 5 5
3 3 3 3 3 3 3

Exercise 2:
8
6 6
4 4 4 
2 2 2 2
0 0 0 0 0


Exercise 3:
4 4 4 4 4 4 4 4
3 3 3 3 3 3
2 2 2 2
1 1

Exercise 4:
2 2 2 2 2 2
4 4 4 4 
6 6


Exercise 5:
1 1 1 1 1 
5 5 5
9

Exercise 6:
5 5
10 10 10 10
15 15 15 15 15 15

Exercise 7:
6 
444
22222 

Exercise 8:
6
444
2222
Find elsewhere
🌐
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.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit4-Iteration › topic-4-4-nested-loops.html
4.4. Nested For Loops — CSAwesome v1
Add an if/else statement that changes the Color of the pen before the inner loop depending on whether the outer loop variable is odd or even. Remember that even numbers have no remainder when divided by 2. Be creative and design a unique snowflake! Use nested for-loops to have the turtle draw a snowflake of polygons.
🌐
Medium
medium.com › @saboorakhalil › mastering-loops-nested-loops-in-python-a-practical-guide-adb2ebb36022
Mastering Loops & Nested Loops in Python: A Practical Guide | by Saboora Khalil | Feb, 2026 | Medium
February 16, 2026 - Both loops are foundational, but choosing between them depends on whether you know the iteration count beforehand. A nested loop is simply a loop inside another loop.
🌐
Medium
medium.com › @friedman.sydneyl › mastering-nested-while-loops-in-python-a-step-by-step-guide-9a0b30b01abd
Mastering Nested While Loops in Python: A Step-by-Step Guide | by Sydney Friedman | Medium
September 10, 2024 - Iterating through elements: We need to compare each number with every other number in the array. Loops are perfect for that. Checking combinations: We’re looking for pairs of numbers. That means using nested loops — one loop to pick the first number and another to check the rest.
🌐
Reddit
reddit.com › r/learnpython › nested loops
r/learnpython on Reddit: Nested loops
November 13, 2022 -

For context,I am currently following Programming with Mosh's beginner python course(for 4 days) and was on the solution to the challenge for the nested loop part.

I have seen that when you type

numbers = [5, 2, 5, 2, 2]

for x_count in numbers:

for count in range(x_count):

print(count)

what is shown are the numbers

0

1

2

3

4

0

1

0

1

2

3

4

0

1

0

1

and then if you type

numbers = [5, 2, 5, 2, 2]

for x_count in numbers:

output = ''

for count in range(x_count):

output += 'x'

print(output)

then it shows up as

xxxxx

xx

xxxxx

xx

xx instead of

x

xx

xxx

xxxx

x

x

xx

xxx

xxxx

x

x

Which is what i thought should happen. Why? I want to understand rather than just mermorise and move on.

Top answer
1 of 3
2
Please read the FAQ to see how to format code so the indentation is maintained. This is probably your code for the second part: numbers = [5, 2, 5, 2, 2] for x_count in numbers: output = '' for count in range(x_count): output += 'x' print(output) That will print each line with the number of xs the same as each number in the list. The list is [5, 2, 5, 2, 2] so you should see lines with 5, 2, 5, 2 and 2 xs, and you do. But you want to print lines that start with one x and increase up to the number one less than the number from the list. Look at your first output example that starts: 0 1 2 3 4 0 ... Instead you want: x xx xxx xxxx ... So instead of 3, for example, you want to print xxx, and instead of 2 you want xx. So instead of printing a number, you want that number of x characters. So replace the line print(count) with something that prints the count number of xs. The hint here is that you can multiply a string by an integer. Try running this bit of code: count = 3 print("x" * count)
2 of 3
1
Both examples have nested loops. The print function in the first example is within the nested loop and thus prints for each element of the range for every element of the original list. The second example only appends x to the output within the nested loop, but doesn’t print until the nested loop ends. Therefore you only get one printed output line for each element in the original list. Now notice how your code formatting is a bit messed up here on Reddit. This is particularly important since white space is what determines what goes under what loop in python. You could try indenting the print statement into the nested loop and you’ll get what you thought you would get.
🌐
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
Top answer
1 of 5
242

The best source of information is the official Python tutorial on list comprehensions. List comprehensions are nearly the same as for loops (certainly any list comprehension can be written as a for-loop) but they are often faster than using a for loop.

Look at this longer list comprehension from the tutorial (the if part filters the comprehension, only parts that pass the if statement are passed into the final part of the list comprehension (here (x,y)):

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

It's exactly the same as this nested for loop (and, as the tutorial says, note how the order of for and if are the same).

>>> combs = []
>>> for x in [1,2,3]:
...     for y in [3,1,4]:
...         if x != y:
...             combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

The major difference between a list comprehension and a for loop is that the final part of the for loop (where you do something) comes at the beginning rather than at the end.

On to your questions:

What type must object be in order to use this for loop structure?

An iterable. Any object that can generate a (finite) set of elements. These include any container, lists, sets, generators, etc.

What is the order in which i and j are assigned to elements in object?

They are assigned in exactly the same order as they are generated from each list, as if they were in a nested for loop (for your first comprehension you'd get 1 element for i, then every value from j, 2nd element into i, then every value from j, etc.)

Can it be simulated by a different for loop structure?

Yes, already shown above.

Can this for loop be nested with a similar or different structure for loop? And how would it look?

Sure, but it's not a great idea. Here, for example, gives you a list of lists of characters:

[[ch for ch in word] for word in ("apple", "banana", "pear", "the", "hello")]
2 of 5
47

You might be interested in itertools.product, which returns an iterable yielding tuples of values from all the iterables you pass it. That is, itertools.product(A, B) yields all values of the form (a, b), where the a values come from A and the b values come from B. For example:

import itertools

A = [50, 60, 70]
B = [0.1, 0.2, 0.3, 0.4]

print [a + b for a, b in itertools.product(A, B)]

This prints:

[50.1, 50.2, 50.3, 50.4, 60.1, 60.2, 60.3, 60.4, 70.1, 70.2, 70.3, 70.4]

Notice how the final argument passed to itertools.product is the "inner" one. Generally, itertools.product(a0, a1, ... an) is equal to [(i0, i1, ... in) for in in an for in-1 in an-1 ... for i0 in a0]

🌐
Medium
medium.com › @danielbuilescu › master-python-loops-for-while-and-nested-loops-bf93ac4cd3a0
Master Python Loops: For, While, and Nested Loops | by Builescu Daniel | Medium
June 14, 2023 - Master Python Loops: For, While, and Nested Loops Welcome, fellow developers! If you’re on a journey to master Python or just want to brush up your skills, you’ve come to the right place. In this …
🌐
Medium
medium.com › @afterhourscoding › mastering-nested-loops-in-python-d46d2b44ee72
Mastering Nested Loops in Python: | by Andres Paniagua | Medium
December 11, 2023 - Nested loops involve placing one loop inside another. They are essential for tasks requiring repeated actions within another set of repeated actions, like working with multi-dimensional arrays or performing complex simulations.
🌐
Softuni
python-book.softuni.org › chapter-06-nested-loops.html
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)....
🌐
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.