Nested For Loop In Python

Nested for loop is a dilemma for the beginners of the Python programming language. Basically, the for loop is used to iterate over given iterable objects like List, Tuple, Dictionary, Set, etc. But if you have more complex types like a list in a list you should use the nested for loops. But the requirement for the nested for loop can be different like you may want to create some joy by using the magic of the nested for loop.

Nested for Loop Syntax

The nested for loop syntax is actually simple where multiple for loops are put by using the Python blocks. There is no limit about the count of the for loop but generally, the 2 or 3 for loop will solve your problem.

for VAR1 in LIST1:
   for VAR2 in LIST2:
      CODE
  • VAR1 is the first level for loop variable which will be selected from the LIST1.
  • LIST1 is the first level list where every item will be set to the VAR1.
  • CODE is the code part of the nested for loop where the current items stored in VAR1 or VAR2 are used.

The nested for loop can be more than 2 levels where there is no limit about the nested for loop count like below.

for VAR1 in LIST1:
   for VAR2 in LIST2:
      for VAR3 in LIST3:
      ...      
         CODE

Nested For Loop Example

In order to fully understand the nested for loops let’s make an example by using a list that contains colors for different cars.

cars = [ "Ferrari" , "BMW" , "Mercedes" ]

colors =[ "red" , "blue" , "yellow" ]


for car in cars:
   for color in colors:
      print(color+" "+car)

The output will be like below.

red Ferrari
blue Ferrari
yellow Ferrari
red BMW
blue BMW
yellow BMW
red Mercedes
blue Mercedes
yellow Mercedes

Use Nested For Loop To Find Prime Numbers

Prime numbers are the numbers which can not be divided numbers without any remaining except itself and 1 . For example 37 is a prime number where it can not be divided without any remaining except 1 and 37. The nested for loop can be used to find the prime numbers.

for i in range(1,100):
   for j in range (1,i):
      if (i%j) == 0:
         pass
      else:
         print(str(i)+" is a prime number")

Use Nested For Loop to Create A Pyramid

You can also use the nested for loop for making some art. This is not a joke you can different structures to the standard output by using the nested loop.

for row in range(10):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()

By increasing the range of the nested for loop bigger structures can be created like below.

for row in range(20):
    for brick in range(row + 1):
         print('#', end = ' ')
    print()

Nested for Loop Oneliner or In A One Line

Up to now we have used the nested loops in multiple lines which is better for readability and examination. But we can also create a nested loop in a one line which is called as oneliner nested for loop.

cars = [ "Ferrari" , "BMW" , "Mercedes" ]

colors =[ "red" , "blue" , "yellow" ]


print("\n".join(color+" "+car for car in cars for color in colors)) 

Leave a Comment