Signup/Sign In
LAST UPDATED: JULY 7, 2021

Python Program to Create Matrix of n*n

In this tutorial, we will learn to create a n*n (n by n) matrix in Python. Where n is the number of rows or columns. n*n matrix has an equal number of rows and columns and is a square matrix.

For creating a matrix in Python, we need multi-dimensional data structures. Python does not have a built-in type for matrices but we can treat a nested list or list of a list as a matrix. The List is an ordered set of elements enclosed in square brackets [ ]. Each element in the list will be then treated as a row of a matrix. For example,

Input: N=2

Output: [ [1,2], [3,4] ]

To create a matrix, we require nested lists, there are different ways for creating a nested list in Python.

  1. By using list comprehension
  2. By using next() and itertools.count() Function

We will be using these methods for creating a n*n matrix.

Approach 1: list comprehension

We will use list comprehension to store rows and columns in the list. List comprehension is a shorter syntax for writing concise codes in Python.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define the dimension of a matrix

Step 2- Use a list comprehension to create a matrix having the defined dimensions

Step 3- Print the resulting matrix

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. This program will work for a 4x4 matrix. We have used .format() to print the values where the placeholder is mentioned.

n = 4
matrix = [list(range(1 + n * i, 1 + n * (i + 1))) for i in range(n)]
print("The created matrix of {} * {}: ".format(n,n))
for m in matrix:
    print(m)


The created matrix of 4 * 4:
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]
[13, 14, 15, 16]

Approach 2: next() and itertools.count()

In our program, we will use the count function to start the count of numbers and the next function will create the consecutive sublists. We will use list comprehension to handles the matrix.

Algorithm

Follow the algorithm to understand the approach better.

Step 1-Import itertools package

Step 2- Define the dimension of the matrix

Step 3- Use count() to start the count of the number

Step 4- Use a list comprehension to store rows and columns in a new list

Step 5-Use next() for creating sublists and store the matrix in the list

Step 6- Print the resulting matrix

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. This program will work for a 5X5 matrix.

import itertools
  
# initializing N
n = 5
temp = itertools.count(1) 
matrix = [[next(temp) for i in range(n)] for i in range(n)]
print("The created matrix of {} * {}: ".format(n,n))
for m in matrix:
    print(m)


The created matrix of 5 * 5:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]

Conclusion

In this tutorial, we have learned two ways for creating square matrices in Python. We have learnt how to use list comprehension for creating a square matrix. We have also seen how to use count() and next() function in itertools package to create a matrix in Python.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.