Python Coding Problems for Nested For Loops
4 min readDec 7, 2024
When students start learning to code, they usually understand the concept of if-else and for-loops quite easily, but where the gravy train gets stuck is nested for-loops. And it makes sense. Nested for-loops is indeed a very complicated concept for beginners. In this blog, I have curated a list of problems on this concept that start from very simple and gradually increase in complexity so that students can be better equipped in solving these problems.
- Print a rectangle of stars (
*
) with 4 rows and 6 columns.
Hint: Use an outer loop for rows and an inner loop for columns.
******
******
******
******
Variation : Take the number of rows and columns from user as input. - Print one star in first row, two stars in second row, and so on.
*
**
***
**** - Print two stars in first row, four stars in second row, and so on.
**
****
******
******** - Print a reverse right-angled triangle of stars.
*****
****
***
**
* - Print a right-angled triangle of numbers where the number of columns in each row equals the row number.
1
12
123
1234
12345 - Generate a 5x5 matrix where each cell contains the product of its row and column indices. So the cell on 3rd row and 4th column will have 12.
Variation : Generalise this to any NxN matrix and take N as input from the user. - Generate an NxM matrix (user inputs N and M) where each cell contains the sum of its row and column indices.
- Print a multiplication table for numbers 1 to 10 using nested loops.
Hint: Use an outer loop for rows (numbers 1–10) and an inner loop for columns (1–10). - Given a list of lists, print all the numbers one by one.
Example: For[[1, 5], [7, 4], [5, 9]]
, the output should be:
1
5
7
4
5
9 - Given a list of lists, print the squares of the numbers one by one.
Example: For[[1, 5], [7, 4], [5, 9]]
, the output should be:
1
25
49
16
25
81 - Flatten a list of lists into a single list.
Example: Convert[[1, 2], [3, 4]]
into[1, 2, 3, 4]
. - Given a list of lists, print the factorials of the numbers one by one.
Hint : Define a separate factorial function and call it from inside the loop.
Example: For[[1, 5], [7, 4], [5, 9]]
, the output should be:
1
120
5040
24
120
362880 - Given a list of lists, find the sum of all elements.
Example: For[[1, 2], [3, 4], [5, 6]]
, the sum is21
.
Hint : You will need to initialise the sum variable carefully. Where will you initialise it? Outside both loops or inside one of them? - Given a list of lists, find the sum of all elements of each individual list.
Example: For[[1, 2], [3, 4], [5, 6]]
, the output should be:
3
7
11
Task : Carefully think about the difference between this and the previous problem, in terms of where to initialise the sum variable. - Given a list of lists, print the number of even numbers in each individual list.
Example: For[[1, 2], [3, 4, 10, 12], [8, 6]]
, the output should be:
1
3
2 - Given a list of lists, print the number of prime numbers in each individual list.
Example: For[[3, 6], [4, 10, 15], [8, 11, 13]]
, the output should be:
1
0
2
Hint : Define another function to check if a given number is prime or not, and call this function from inside your loop. - Print all pairs of elements from two lists, e.g.,
[1, 2]
and[3, 4]
.
(1, 3)
(1, 4)
(2, 3)
(2, 4) - Print the product of all pairs of elements from two lists, e.g.,
[1, 2]
and[3, 4]
.
3
4
6
8 - Given two lists containing unique elements, count how many elements from the first list appear in the second list.
Example Input:[1, 2, 3]
and[2, 3, 4]
.
Output:2
(since2
and3
are common). - Given a list of lists, find the maximum value in each individual list.
Example: For[[1, 2], [3, 4, 5], [6]]
, the output should be:
2
5
6 - Given a 3x3 matrix, print its transpose.
- Calculate the sum of the diagonal elements of a 3x3 matrix.
- Generate an NxN matrix with alternating 0s and 1s, starting with 0.
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0 - Given a list of distinct numbers, return another list which contains the sum of all pairs of numbers in the given list.
- Given a list of distinct numbers (may contain zero), return another list which contains the ratio of all pairs of numbers in the given list.
- Given a list of positive integers, return a list of the factorial of all these numbers without defining a separate function for factorials.
- Given a positive integer, return a list of all prime numbers from 1 up to this number without defining a separate function for prime number checking.
- Given a positive integer, return the sum of all prime numbers from 1 up to this number without defining a separate function for prime number checking.
- Given a list of integers, return its mode (list of numbers with highest frequency of occurrence). Do not use a dictionary.
- Given a list of lists of integers, return a list that is sorted based on the sum of each inner list. Do not use any inbuilt function for sorting.