Python Coding Problems for Nested For Loops

Kushal Shah
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.

  1. 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.
  2. Print one star in first row, two stars in second row, and so on.
    *
    **
    ***
    ****
  3. Print two stars in first row, four stars in second row, and so on.
    **
    ****
    ******
    ********
  4. Print a reverse right-angled triangle of stars.
    *****
    ****
    ***
    **

    *
  5. Print a right-angled triangle of numbers where the number of columns in each row equals the row number.
    1
    12
    123
    1234
    12345
  6. 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.
  7. Generate an NxM matrix (user inputs N and M) where each cell contains the sum of its row and column indices.
  8. 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).
  9. 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
  10. 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
  11. Flatten a list of lists into a single list.
    Example: Convert [[1, 2], [3, 4]] into [1, 2, 3, 4].
  12. 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
  13. Given a list of lists, find the sum of all elements.
    Example: For [[1, 2], [3, 4], [5, 6]], the sum is 21.
    Hint : You will need to initialise the sum variable carefully. Where will you initialise it? Outside both loops or inside one of them?
  14. 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.
  15. 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
  16. 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.
  17. Print all pairs of elements from two lists, e.g., [1, 2] and [3, 4].
    (1, 3)
    (1, 4)
    (2, 3)
    (2, 4)
  18. Print the product of all pairs of elements from two lists, e.g., [1, 2] and [3, 4].
    3
    4
    6
    8
  19. 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 (since 2 and 3 are common).
  20. 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
  21. Given a 3x3 matrix, print its transpose.
  22. Calculate the sum of the diagonal elements of a 3x3 matrix.
  23. 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
  24. Given a list of distinct numbers, return another list which contains the sum of all pairs of numbers in the given list.
  25. 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.
  26. Given a list of positive integers, return a list of the factorial of all these numbers without defining a separate function for factorials.
  27. 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.
  28. 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.
  29. Given a list of integers, return its mode (list of numbers with highest frequency of occurrence). Do not use a dictionary.
  30. 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.

--

--

Kushal Shah
Kushal Shah

Written by Kushal Shah

Studied at IIT Madras, and was earlier faculty at IIT Delhi. Learn coding my Python Pal : https://www.pythonpal.org

No responses yet