59. Problem to create a 3×3 grid with numbers.

In this Python list program, we will create a 3*3 grid with numbers in a list using the Python programming language.

Create a 3×3 grid with numbers in list:

Steps to solve the program
  1. Create an empty list.
  2. Use a for loop to iterate over numbers from 0 to 2.
  3. In each iteration add an empty list to the created empty list.
  4. Use a nested for loop to iterate over numbers from 4 to 6.
  5. Add those numbers to the empty that we have created in the previous for loop.
  6. Print the list to see the output i.e. 3×3 grid with numbers
				
					#Input list
list1 = []

for i in range(3):
    list1.append([])
    for j in range(4,7):
        list1[i].append(j)
        
#Printing output
print("3X3 grid: ",list1)
				
			

Output :

				
					3X3 grid:  [[4, 5, 6], [4, 5, 6], [4, 5, 6]]
				
			

Related Articles

Select random numbers from the list

Zip two lists of lists into a list

Leave a Comment