In this Python tutorial, we’ll learn Python nested for loop in detail. We’ll understand this concept programmatically using practical Python code examples. We’ll explain these examples step by step for better understanding.
Introduction: Python Nested For Loop
We can also call this concept as loop inside loop. In this method, we’ve a for loop and inside it, we’ve another for loop. This for loop is called nested for loop. We can also add more for loops like this.
Let’s now understand it through proper practical Python examples.
Syntax of Nested For Loop
for val in value: for v in values:
This is how we can create Python nested for loop. We can add more loops as well.
Implementing Python Nested For Loop (Easy Examples)
Below examples will practically demonstrate the use of nested for loops in Python.
Example 1: Addition of List Using Python Nested For Loop
list1=[1,2,3,4,5] list2=[2,4,6,8] listSum=[] for val1 in list1: for val2 in list2: listSum.append(val1+val2) print(listSum)
Output
[3, 5, 7, 9, 4, 6, 8, 10, 5, 7, 9, 11, 6, 8, 10, 12, 7, 9, 11, 13]
- We’ve created two Python lists.
- Also, we’ve created an empty list to store the sum.
- After that, we’ve used a Python for loop which in its first execution, will take the first item of the first list. In second execution, it will take the second item and so on.
- We also have created a nested for loop which will take each and every item of the second list just like the first loop.
- Difference between these two loops are that in every execution, one item of first list but every item of second list will be used.
- We’ve used append method of Python list to add the sum in our new list. As a result, our empty list has stored all the sum values which can be seen in the above output block.
Example 2: Print Pattern Using Nested For Loop
for val in range(10): for val2 in range(val): print('*', end = '') // end is used to force the print function to not go to second line print() // this print is used to break the line
Output
* ** *** **** ***** ****** ******* ******** *********
- We’ve created a for loop with range of 10.
- Also, we’ve created a Python nested for loop which will take each item of the above for loop as its range.
- First range is 0 as range(10) means 0,1,2,3,4,5,6,7,8,9. Second range is 1 and so on.
- The maximum asterisk(*) is only 9. Reason is that range(10) means from 0,1,2,3,4,5,6,7,8,9. So the 10th range was 9.
- We want you to give it a try and make more beautiful patterns using nested for loop.
Example 3: Multiple Nested For Loop
for val1 in range(10): for val2 in range(val1): for val3 in range(val2): print('*',end='') print()
Output
* * ** * ** *** * ** *** **** * ** *** **** ***** * ** *** **** ***** ****** * ** *** **** ***** ****** ******* * ** *** **** ***** ****** ******* ********
We can use multiple nested for loops as well which can be seen in the above program. You can use it to perform any specific action.
Example 4: Table of 16 Using Nested For Loop
for val1 in range(16,17): for val2 in range(1,11): print( val1, 'x' , val2, '=' ,( val1*val2 ) )
Output
16 x 1 = 16 16 x 2 = 32 16 x 3 = 48 16 x 4 = 64 16 x 5 = 80 16 x 6 = 96 16 x 7 = 112 16 x 8 = 128 16 x 9 = 144 16 x 10 = 160
- First for loop will take a rang of only 16 as range(16,17) is equal to range (16,17-1).
- Second range will be from 1 to 10. Reason is range(1,11) is equal to range(1,11-1).
- We’ve used multiplication operator to multiply the value. The result can be shown in the above output block.
- You can try it with other tables as well.
So this is how we can easily use Python nested for loop. Hope you’ve learned a lot from this tutorial. Do feel free to share it with other Python programmers.
Also, don’t hesitate to ask if you still have any doubts regarding Python nested for loop. We’ll be very happy to clear all your doubts.
Conclusion
In conclusion, hope you now have an in-depth practical understanding of how to easily use Python nested for loop. We’d be super happy to have your amazing feedback on this post.
Feel free to visit our other posts on Python programming to learn important Python concepts. Thank you for reading this article.