Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - NESTED LOOP


So far we have learnt, 'while loops' and 'for loops'.


Well! They can be combined together, one inside the other. And this is called 'Nested Loops'.A loop inside a loop.


Let us understand with the below example.


Say, we want to write a program that prints the below output :


Output :



   1   John
   1   Sam
   2   John
   2   Sam

Let us write the code for the same using a 'for loop' inside 'while loop' i.e. 'Nested Loops'.


Example :


x = 1 
names = ["John", "Sam"]
while x <= 2:
    for y in names:
        print(x, y)
    x = x + 1
	


Output :



   1   John
   1   Sam
   2   John
   2   Sam

So, in the above code we have two loops.

  1. The 'while loop'
  2. And the 'for loop'

Let us see in detail, how the loops work.


So, we have declared a variable names 'x' to maintain the 'while loop'.


x = 1

That stores the initial value as '1'.


java_Collections

Then we have declared a list named 'names' for the 'for loop'.


names = ["John", "Sam"]

Which has the names, 'John' and 'Sam'.


java_Collections

Then we have the 'while loop',


while x <= 2:

And the control checks, if the value of 'x' is less than or equal to '2'. In this case the value of 'x' is '1'.


So, the control enters the 'while loop'.


1st Iteration of 'while loop'


During the first iteration of 'while loop', we find the 'for loop' inside the 'while loop'.


for y in names:

And we start the iteration of 'for loop' inside the '1st Iteration of while loop'.


1st Iteration of 'for loop' inside the 1st Iteration of 'while loop'


And in the 1st Iteration of 'for loop', we find the print statement.


print(x, y)

If we check the current value of 'x', it is '1'.


java_Collections

And in the first iteration of 'for loop', the first value from the 'names' list is taken and placed into the variable 'y'.


java_Collections

So, we got the value of 'x' and 'y'. And the output is,


Output :



   1   John

And we are done with the first iteration of 'for loop'. And begin the second iteration of 'for loop'.


2nd Iteration of 'for loop' inside the 1st Iteration of 'while loop'


Now, in the 2nd Iteration of 'for loop', we find the print statement.


print(x, y)

Similarly, we get the current value of 'x', which is still '1'.


java_Collections

And in the second iteration of 'for loop', the second value from the 'names' list is taken and placed into the variable 'y'. Which is 'Sam'.


java_Collections

So, we got the value of 'x' and 'y'. And the output is,


Output :



   1   John
   1   John

So, we are done with the execution of the inner 'for loop'. And we get out of the 'for loop'.


And we encounter the line,


x = x + 1

Where we have incremented the value of 'x' by '1'.


java_Collections

Then we come back to the 'while loop'.


while x <= 2:

And the condition of 'while loop' satisfies as the value of 'x' is '2'. So, we start the second iteration of 'while loop'.


2nd Iteration of 'while loop'


Similarly, during the second iteration of 'while loop', we find the 'for loop' inside the 'while loop'.


for y in names:

And we start the iteration of 'for loop' inside the '2nd Iteration of while loop'.


1st Iteration of 'for loop' inside the 2nd Iteration of 'while loop'


And in the 1st Iteration of 'for loop', we find the print statement.


print(x, y)

If we check the current value of 'x', it is '2'.


java_Collections

And in the first iteration of 'for loop', the first value from the 'names' list is taken and placed into the variable 'y'.


java_Collections

So, we got the value of 'x' and 'y'. And the output is,


Output :



   1   John
   1   Sam
   2   John

And we are done with the first iteration of 'for loop'. And begin the second iteration of 'for loop'.


2nd Iteration of 'for loop' inside the 1st Iteration of 'while loop'


Now, in the 2nd Iteration of 'for loop', we find the print statement.


print(x, y)

Similarly, we get the current value of 'x', which is still '2'.


java_Collections

And in the second iteration of 'for loop', the second value from the 'names' list is taken and placed into the variable 'y'. Which is 'Sam'.


java_Collections

So, we got the value of 'x' and 'y'. And the output is,


Output :



   1   John
   1   Sam
   2   John
   2   Sam

So, we are done with the execution of the inner 'for loop'. And we get out of the 'for loop'.


And we encounter the line,


x = x + 1

Where we have incremented the value of 'x' by '1'.


java_Collections

Then we come back to the 'while loop'.


while x <= 2:

And the condition of 'while loop' fails, as the value of 'x' is '3'. And the execution ends.


So, we have learnt, how the execution of a 'for loop' inside a 'while loop' works.


Now, let us look at a little complex example, of a Nested 'for loop'. i.e. A 'for loop' inside a 'for loop'.


Say, we have a list that has two names, 'Sam' and 'Jack'.


Now, we need to print each letter of the name, as stated below :



  S
  a
  m
  J
  a
  c
  k

Let us explain with the below example.


Example :


names = ["Sam", "Jack"]
for x in names :
    for y in x :
        print(y)


Output :



  S
  a
  m
  J
  a
  c
  k

So, if we look at the above code, we have created a list named 'names',


names = ["Sam", "Jack"]

And stored the names, 'Sam' and 'Jack' in it.


java_Collections

Now, our main motto is, to print each letters of the names. And for that we have used two 'for loops'.

  1. Outer 'for loop'
  2. And the Inner 'for loop'

So, we come to the next line where the execution of Outer 'for loop' begins.


for x in names :

1st Iteration of the Outer 'for loop'


So, in the 1st Iteration of the Outer 'for loop', we get the first element(i.e. 'Sam') from the list 'names' and store it in the variable 'x'.


java_Collections

Then we come to the inner 'for loop'.


for y in x :

1st Iteration of the Inner 'for loop'


Now, in the 1st Iteration of the Inner 'for loop', we collect the value from 'x', one by one and store in the variable 'y'.


Since, there is 'Sam' stored in 'x'. The first element of 'x' (i.e. 'S') is taken and stored in 'y'.


java_Collections

Then we encounter the print statement.


print(y)

Where we print the value of 'y'.


Output :



  S

Then we start the second Iteration of the Inner 'for loop'.


2nd Iteration of the Inner 'for loop'


Similarly, in the 2nd Iteration of the Inner 'for loop', we collect second value from 'x', and store it in the variable 'y'.


Since, there is 'Sam' stored in 'x'. The second element of 'x' (i.e. 'a') is taken and stored in 'y'.


java_Collections

Then we encounter the print statement.


print(y)

Where we print the value of 'y'.


Output :



  S
  a

Then we start the third Iteration of the Inner 'for loop'.


1st Iteration of the Inner 'for loop'


Similarly, in the 3rd Iteration, we collect third value from 'x', and store it in the variable 'y'.


java_Collections

And print the value of 'y'.


Output :



  S
  a
  m

And we come to an end of the Inner 'for loop' and go back to the outer 'for loop'.


2nd Iteration of the Outer 'for loop'


Again, in the 2nd Iteration of the Outer 'for loop', we get the second element(i.e. 'Sam') from the list 'names'.


java_Collections

And store it in the variable 'x'.


java_Collections

Then we come to the inner 'for loop'.


for y in x :

1st Iteration of the Inner 'for loop'


Now, in the 1st Iteration of the Inner 'for loop', we collect the value from 'x', one by one and store in the variable 'y'.


Since, there is 'Jack' stored in 'x'. The first element of 'x' (i.e. 'J') is taken and stored in 'y'.


java_Collections

Then we encounter the print statement.


print(y)

Where we print the value of 'y'.


Output :



  S
  a
  m
  J

Output :



  S
  a
  m
  J
  a
  c
  k