Skip to content

Nested For Loops in Python Vijetha Spark By {Examples}

  • by

How to implement nested for loops in Python? You can implement nested for loops using various techniques of Python. Python provides two types of loops which are for loop and while loop. You can use these loops to create a nested loop. A loop inside the other loop is called a nested loop. 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.

In this article, I will explain the concept of nested for loops and how they can be implemented using various methods of Python with examples.

1. Quick examples of Nested For Loops

Following are the quick examples of Nested for loops.

# Quick examples of nested for loops

# Example 1: Implement the Nested loop using range()
# Outer loop
for i in range(2, 6):
print(“First 10 Multiples of :”, i)
# Inner loop
for j in range(1,11):
print(i*j, end=”, “)
print(“n”)

# Example 2: Get square pattern of star
# using nested for loops
# Outer loop
for i in range(5):
# inner loop
for j in range(5):
print(“*”, end=” “)
print(”)

# Example 3: Implementation of while loop inside for loop
# Initialize the list
list = [“Spark”, “by”, “EXamples”]
# Outer loop
for i in list:
# inner while loop
count = 0
while count < len(list):
print(i, end=’ ‘)
# increment counter
count = count + 1
print()

# Example 4: Append list using nested for loops
# Initialize the lists
list1 = [10, 20]
list2 = [30, 40]
list3 = []
for i in list1:
for j in list2:
list3.append(i + j)
print(“Appended list:”, list3)

# Example 5: Append list using nested for loops & list comprehension
list1 = [10, 20]
list2 = [30, 40]
final_list = [i+j for i in list1 for j in list2]
print(“Final list:”, final_list)

# Example 6: Using break statement in nested loops
# Outer loop
for i in range(2, 6):
# Inner loop
for j in range(1,11):
if i == j:
break
print(i*j, end=”, “)
print(“n”)

# Example 7: Using continue statement in nested loops
# Outer loop
for i in range(2, 6):
# Inner loop
for j in range(1,11):
if i == j:
continue
print(i*j, end=”, “)
print(“n”)

2. Nested For Loops

A loop contains inside the other loop is called a nested for loop. The inner loop completes all its iterations for every iteration of the outer loop. Both inner and outer loops can be any kind of loop, which means you can use the while loop inside the for loop and vice versa. The outer loop can contain more than one inner loop. There is no limitation on the chaining of loops.

2.1 Syntax of Nested For Loops

Following is the syntax of nested for loop.

3. Implement the Python Nested For Loops

You can use nested for loops with the range() function to get the first 10 multiples of the specified range of numbers. First, specify the range of numbers(these numbers multiples we want to get) in the outer loop and specify a range of 10 numbers in the inner loop. For every outer loop iteration, the inner loop will execute 10 times. Finally, you can get the first 10 multiples of specified numbers.

# Implement the Nested loop using range()
# Outer loop
for i in range(2, 6):
print(“First 10 Multiples of :”, i)
# Inner loop
for j in range(1,11):
print(i*j, end=”, “)
print(“n”)

Yields below output.

4. Get Pattern using Nested For Loop

You can use nested for loops to get the different patterns of stars and numbers. In this example, I will create a square pattern of stars using nested for loop and range() function.

# Get square pattern of star
# using nested for loops
# Outer loop
for i in range(5):
# inner loop
for j in range(5):
print(“*”, end=” “)
print(”)

Yields below output.

5. While Loop Inside the For Loop

You can implement nested loops using a while loop inside the for loop. Like for loop, while loop also iterates over the iterable objects such as a list, tuple, set, dictionary, string, and array until a certain condition is met.

Below example, we will iterate the given list using an outer loop. For every iteration of outer loop inner loop will execute three times and print each element present.

# Implementation of while loop inside for loop
# Initialize the list
list = [“Spark”, “by”, “EXamples”]

# Outer loop
for i in list:
# inner while loop
count = 0
while count < len(list):
print(i, end=’ ‘)
# increment counter
count = count + 1
print()

# Output:
# Spark Spark Spark
# by by by
# EXamples EXamples EXamples

6. Append the Lists Using Nested For Loops

Alternatively, you can use nested for loop to append two lists. First, create two lists and iterate them using the inner loop and outer loop. For every iteration of the outer loop inner loop will execute completely and append elements to the empty list using the append() function. For example,

# Append list using nested for loops
# Initialize the lists
list1 = [10, 20]
list2 = [30, 40]
list3 = []
for i in list1:
for j in list2:
list3.append(i + j)
print(“Appended list:”, list3)

# Output:
# Appended list: [40, 50, 50, 60]

7. Implement Nested Loops Using List Comprehension

You can implement nested for loops using list comprehension. Use nested for loops with a list comprehension to append two lists. Using list comprehension with nested loops you can create a new list from an existing list in a concise way.

# Append list using nested for loops & list comprehension
list1 = [10, 20]
list2 = [30, 40]
final_list = [i+j for i in list1 for j in list2]
print(“Final list:”, final_list)

# Output:
# Final list: [40, 50, 50, 60]

 8. Nested For Loop Using Break Statement

You can use a break statement to implement the nested for loops. The break statement is one of the control statements which is used to terminate from the for loop/while loop. You can use a break statement in a nested loop to exit the innermost loop.

In the following example, the outer for loop iterates a specified range of sequences using the range() function and the inner loop also iterates another range of sequences. If the outer loop number and inner loop number are the same break the inner loop and continue the outer loop.

# Using break statement in nested loops
# Outer loop
for i in range(2, 6):
# Inner loop
for j in range(1,11):
if i == j:
break
print(i*j, end=”, “)
print(“n”)

# Output:
# 2,

# 3, 6,

# 4, 8, 12,

# 5, 10, 15, 20,

9. Nested For Loop using Continue Statement

You can use a continue statement to implement the nested for loops. The continue statement is one of the control statements which is used to skip the current iteration and go to the next iteration. You can use the continue statement in a nested loop to skip the current iteration and go to the next iteration of an inner loop.

In the following example, the outer for loop iterates a specified range of sequences using the range() function and the inner loop also iterates another range of sequences. If the outer for loop number and inner loop number is the same skip the inner loop current iteration and jump to the inner loop next iteration.

# Using continue statement in nested loops
# Outer loop
for i in range(2, 6):
# Inner loop
for j in range(1,11):
if i == j:
continue
print(i*j, end=”, “)
print(“n”)

# Output:
# 2, 6, 8, 10, 12, 14, 16, 18, 20,

# 3, 6, 12, 15, 18, 21, 24, 27, 30,

# 4, 8, 12, 20, 24, 28, 32, 36, 40,

# 5, 10, 15, 20, 30, 35, 40, 45, 50,

10. Conclusion

In this article, I have explained the concept of nested for loops and using different methods of Python how we can implement the nested for loops. And explained using break and continue statements in nested loops how we can control the innermost loops with examples.

Happy Learning!!

 How to implement nested for loops in Python? You can implement nested for loops using various techniques of Python. Python provides two types of loops which are for loop and while loop. You can use these loops to create a nested loop. A loop inside the other loop is called a nested loop. The inner  Read More Python, Python Tutorial, Pending review by admin, Python For Loop Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *