Nested For Loop
Python For Loop is just like another Python command or statement. Therefore, we can write a for loop inside another for loop and this is called nesting.
For loop inside another for loop is called Nested For Loop.
Print Triangular Pattern
In the following program, we shall write a nested for loop, to print a triangular pattern.
n = 6 for x in range(n): for y in range(x+1): print('* ', end=' ') print()
* * * * * * * * * * * * * * * * * * * * *
Iterate over List of Lists
In the following program, we shall write a nested for loop, to iterate over a List of Lists.
input = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ] for inner in input: for element in inner: print(element)
a b c d e f
In this tutorial of Python Examples, we learned how to write nested for loops with examples.
- How to Print Information of Pandas DataFrame?
- Python Program for NLTK Stemming
- Pandas DataFrame – Get Axes Information
- Python List For Loop
- Python Projects for Beginners
- Python for i in range()
- Fibonacci Series in Python using For Loop
- Python Tuple For Loop
- Python For Loop
- How to create Login Form using Python Tkinter?