Tutorials Tonight
Follow Us

Python Nested While Loop


In this article, we will learn about Python Nested While Loop with various examples.

Python While loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when we talk about nested while loop, it means that a while loop is present inside another while loop.

A nested while loop is a loop inside a loop. The inner loop will be executed completely for each iteration of the outer loop.

Let's see the syntax of a nested while loop in Python:

while condition1:
    while condition2:
        # statement(s)
    # statement(s)

The inner loop will execute for each iteration of the outer loop. For simple calculation, if the outer loop runs 5 times, and the inner loop runs 3 times, then the inner loop will run for 5 × 3 = 15 times in total.

python nested while loop
Python Nested While Loop

Python Nested While Loop Example

Let's see a simple example of a nested while loop in Python to understand it better.

Example 1

Here is a simple example that shows how a nested while loop works in Python.

i = 1
while i <= 3:
    print("Outer Loop: ", i, "time ------------------")
    j = 1
    while j <= 2:
        print("Inner Loop:", j)
        j += 1
    i += 1

Output:

Outer Loop:  1 time ------------------
Inner Loop: 1
Inner Loop: 2
Outer Loop:  2 time ------------------
Inner Loop: 1
Inner Loop: 2
Outer Loop:  3 time ------------------
Inner Loop: 1
Inner Loop: 2

In the above example, you can clearly see that the inner loop is executed for each iteration of the outer loop.


Example 2

Let's say we want to print the table up to 5. We can use a nested while loop to do this.

i = 1
# outer loop
while i <= 5:
    j = 1
    # inner loop
    while j <= 10:
        print(i * j, end=" ")
        j += 1
    print()
    i += 1

Output:

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50

Here, the outer loop runs 5 times and for each iteration of the outer loop, the inner loop runs 10 times and prints the table of that number.


Example 3

Using nested while loop to print elements of a list of lists.

# nested while loop to print elements of a list of lists
list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

i = 0
# outer loop
while i < len(list1):
    j = 0
    # inner loop
    while j < len(list1[i]):
        print(list1[i][j], end=" ")
        j += 1
    print()
    i += 1

Output:

1 2 3
4 5 6
7 8 9

The outer loop runs for the number of elements in the list and for each iteration of the outer loop, the inner loop runs for the number of elements in the inner list.


Example 4

Printing a pyramid pattern using nested while loop.

size = 5
i = 0
# outer loop
while i < size:
    j = 0
    # inner loop to print spaces
    while j < size - i - 1:
        print(' ', end='')
        j += 1
    k = 0
    # inner loop to print stars
    while k < 2 * i + 1:
        print('*', end='')
        k += 1
    print()
    i += 1

Output:

    *
   ***
  *****
 *******
*********

Example 5

You can use nested while loop to sort a list of numbers in ascending order.

list1 = [5, 3, 8, 6, 7, 2]

i = 0
# outer loop
while i < len(list1):
    j = i + 1
    # inner loop
    while j < len(list1):
        if list1[i] > list1[j]:
            temp = list1[i]
            list1[i] = list1[j]
            list1[j] = temp
        j += 1
    i += 1

print(list1)

Output:

[2, 3, 5, 6, 7, 8]

Example 6

This is an example of a nested while loop inside a nested while loop.

i = 1
# outer loop
while i <= 2:
    print("Outer Loop ", i, "times")
    j = 1
    # middle loop
    while j <= 2:
        print("    Middle Loop:", j)
        k = 1
        # inner loop
        while k <= 2:
            print("        Inner Loop:", k)
            k += 1
        j += 1
    i += 1

Output:

Outer Loop  1 times
    Middle Loop: 1
        Inner Loop: 1
        Inner Loop: 2
    Middle Loop: 2
        Inner Loop: 1
        Inner Loop: 2
Outer Loop  2 times
    Middle Loop: 1
        Inner Loop: 1
        Inner Loop: 2
    Middle Loop: 2
        Inner Loop: 1
        Inner Loop: 2

Stay Ahead, Learn More.


Nested While Loop with Break and Continue

break and continue statements are used to control the flow of the loop. You can use them in nested while loop as well.

  1. break statement is used to terminate the loop.
  2. continue statement is used to skip the current iteration of the loop.

Let's see some examples.


Loop through a list of lists using break

Let's say we want to print the elements of a list of lists. We can use a nested while loop to do this. But what if we want to stop the loop when we reach a particular element? We can use break statement to do this.

list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

i = 0
# outer loop
while i < len(list1):
    j = 0
    # inner loop
    while j < len(list1[i]):
        # break the loop when we reach 5
        if list1[i][j] == 5:
            break
        print(list1[i][j], end=" ")
        j += 1
    print()
    i += 1

Output:

1 2 3
4
7 8 9

Here, the inner loop breaks when it reaches 5. But outer loop continues to run and restarts the inner loop for the next element in the outer list.


Loop through a list of lists using continue

With the help of continue statement, we can skip the current iteration of the loop.

The following program prints the elements of a list of lists except 5 and 7.

list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

i = 0
# outer loop
while i < len(list1):
    j = 0
    # inner loop
    while j < len(list1[i]):
        # skip the current iteration when we reach 5 or 7
        if list1[i][j] == 5 or list1[i][j] == 7:
            j += 1
            continue
        print(list1[i][j], end=" ")
        j += 1
    print()
    i += 1

Output:

1 2 3
4 6
8 9

Conclusion

So, this is how you can use nested while loop in Python. You can use nested while loop to do a lot of things.