How to Create a Python Empty Matrix [3 ways]

In this Python tutorial, you will learn “How to Create a Python Empty Matrix” using various techniques and practical examples.

The matrix is generally used in statistical calculations, machine learning, etc. But before that, you have to understand the basics of the matrix, so here I will teach you “how to create an empty matrix in Python”.

What is Matrix in Python?

In every programming language, the matrix is also known as a 2D array, where we can arrange the data in rows and columns. We can create a matrix in Python using the list of lists or the numpy library.

But what if you want to create an empty matrix in Python? If you try to create an empty matrix manually like this:

matrix = [[None, None, None] , [None, None, None].........]

Then it will take so much of your time, and it is not good practice to do these things manually, especially in Python, to get this type of output

[None, None, None]
[None, None, None]
[None, None, None]

3 ways to create a Python empty matrix

So we found 3 solutions. In each, you provide the number of rows and columns as an input, and the solution creates a Python empty matrix.

Let’s understand both techniques one by one with practical examples:

Python Create Empty Matrix using List Comprehension

We can create an empty matrix using List Comprehension in Python with None Value.

READ:  How to update column values in Python Pandas [8 examples]

In this example, we will take user input to make the program dynamic. The for loop will iterate as you give the row range and create columns with None values as you provide input on columns.

This process will automate the manual creation of an empty list. Let’s understand this with a practical example of creating an empty Python matrix.

row = int(input("Enter the number of Rows: "))
col = int(input("Enter the number of Columns: "))

empty_matrix = [[None]*col for i in range(row)]

for i in range(len(empty_matrix)):
    print(empty_matrix[i])
Python Create Empty Matrix using List Comprehension

In the above code, we are initializing two variables named row and col, where we will take user input to get the values.

Then we initialize list comprehension in the empty_matrix variable, where we are using for loop “for i in range(row)]” and giving range as a row and getting None values as per col value like this “[None]*col“.

Create an Empty Matrix in Python using numpy.zeros()

Here, we will use the zeros() method from the numpy module, and before using the method, you have to install the package using this command :

pip install numpy

Now you can import numpy and use numpy.zeros() method. This function will create an empty Python matrix containing zeros as a value.

Syntax

numpy.zeros(shape, dtype=float, order='C')
  • numpy.zeros(shape, dtype=float, order=’C’): In the syntax, you can give the shape like this (2,3), so it will take 2 as a matrix row and 3 as a matrix column.
  • (dtype=float, order=’C’): If you don’t include these parameters in the zeros() method, it will use these default values.

Let’s understand how we can create an empty Python matrix using numpy.zeros() method.

import numpy as np 

row = int(input("Enter how many Rows you want : "))
col = int(input("Enter the number of columns : "))

matrix = np.zeros((row, col))

print('Matrix : \n', matrix)

print("Shape of the Matrix :", matrix.shape)
Create an Empty Matrix in Python using numpy.zeros()

In the above code, we take the user input for rows and columns to make the program dynamic. Then, we use the np.zeros() method, which gives the program a shape like this: “matrix = np.zeros((row, col))”.

READ:  How to View Uploaded Files in Django

How to create a Python empty matrix using numpy.empty()

You can also use the empty() method of the numpy module. This method is used to create the shape of the matrix, and it generates random values in the matrix.

You will use this method to create a Python empty matrix by generating the shape of the empty matrix.

Syntax

 numpy. empty(shape, dtype = float, order = 'C')
  • numpy.empty(shape, dtype = float, order = ‘C’): The dtype and order parameters are the default in the empty method, and you have to give shape as row and col.
import numpy as np 

row = 0
col = 5

matrix = np.empty((row, col)) 
print('Empty Matrix :', matrix) 
print('Shape of Matrix :', matrix.shape) 
write a program to create an empty matrix in Python

In this code, we are giving the values row and col as 0 and 5 and using the empty() method, creating the ” np.empty((row, col)” shape of the empty matrix in Python.

What if you give a shape like row = 2 and col = 3 instead of zero? Then, it will generate random values in the matrix, which you can change according to your requirements.

how to create an empty matrix in python

Conclusion

In this Python article, you learned how to create a Python empty matrix using 3 different methods, including list comprehension and numpy module methods like numpy.zeros() and numpy.empty().

You may like to read: