A Matrix is a data structure, which is two-dimensional or more and stocks the numbers in a row and column format, much like a table. An in-built type for matrices is not available in Python but matrices can be created by making a list of lists and treating it as a matrix.

This tutorial demonstrates the different methods available to us to initialize matrix in Python.

Table of Contents

## Using the `for`

loop to initialize matrix in Python.

The simpler way to initialize matrix in Python is by using the `for`

loop, which is basic, easy, and is something that most people in the programming world have an idea about, which makes it easy to understand and implement for most programmers in real life.

The new matrix would contain several lists that are created and are further assigned to rows or columns as per the size specified by the programmer.

The following code uses the `for`

loop to initialize a two-dimensional matrix in Python.

1 2 3 4 5 6 7 8 9 10 11 |
rsize = int(input("Row size? -> ")) csize = int(input("Column size? -> ")) x = [] y = [] for j in range(0, column_size): y.append(0) for i in range(0, row_size): x.append(y) print(x) |

The above code provides the following output:

1 2 3 4 5 |
Row size? -> 3 Column size? -> 2 [[0, 0], [0, 0], [0, 0]] |

##### Explanation

- Two variables are declared, whose values are taken as an input for row size and column size respectively from the user.
- Two one-dimensional lists
`x`

and`y`

are declared, both as empty lists. - Then, we use
`for`

loop and initialize the column first. - Then, the rows are initialized in the same way, and the columns are appended to rows.
- Finally, the created array is displayed using the
`print`

statement and it is two-dimensional in size.

## Further reading:

## Using `list comprehension`

to initialize matrix in Python.

List comprehension is a faster, shorter, and graceful way to generate lists that are to be created on the basis of the given values of a list that already exists in the Python code.

When it comes to this operation, `list comprehension`

can be utilized as it requires minimal effort if you have the correct understanding of this methodology. As the name suggests, the function is totally based on the creation of lists.

`List comprehension`

is a shorthand for this problem that is faster than the vintage and time-consuming `for`

loop and assists the programmers in the field of dynamic programming with its rapid timings.

The following code uses `list comprehension`

to initialize a two- dimensional matrix in Python.

1 2 3 4 5 6 |
Y = 3 X = 2 matx = [ [ 0 for i in range(X) ] for j in range(Y) ] print("The matrix after initializing : " + str(matx)) |

The above code provides the following output:

1 2 3 |
The created matrix -> [[0, 0], [0, 0], [0, 0]] |

##### Explanation

- The rows and columns are declared by the variables
`Y`

and`X`

respectively. - List comprehension is utilized to initialize a two-dimensional matrix.
- The resultant matrix is then displayed on the output screen.

## Using `list comprehension`

along with the `*`

operator to initialize matrix in Python.

By simply using a `*`

operator along with `list comprehension`

, the time taken for the program can be reduced slightly. We can use the `*`

operator wither once or twice along with `list comprehension`

to initialize matrix in Python.

- The following code uses
`list comprehension`

along with the`*`

operator once to initialize a two-dimensional matrix in Python.

1 2 3 4 5 6 |
Y = 3 X = 2 matx = [ [0 for i in range(X)]] * Y print("The created matrix -> " + str(matx)) |

The above code provides the following output:

1 2 3 |
The created matrix -> [[0, 0], [0, 0], [0, 0]] |

- The following code uses
`list comprehension`

along with the`*`

operator twice to initialize a two-dimensional matrix in Python.

1 2 3 4 5 6 |
Y = 3 X = 2 matx = [[0] * X] * Y print("The created matrix -> " + str(matx)) |

The above code provides the following output:

1 2 3 |
The created matrix -> [[0, 0], [0, 0], [0, 0]] |

Both these codes have the same explanation as that of the `list comprehension`

code without any `*`

operator. The only difference is that the use of `*`

operator slightly reduces the time taken in the compilation of the code.

## Using `NumPy`

to initialize matrix in Python.

`NumPy`

is an abbreviation for Numerical Python, which is a library that is mainly utilized and focused on dealing with matrices and arrays in Python.

The `numpy.array()`

method is mainly utilized to create and delete arrays in Python. It directly takes a list or a list of lists as an argument and returns a matrix. Here, we can also use the `np.zeros()`

function which initializes all elements of a given matrix to zero.

To successfully implement this method, we will first need to import the `NumPy`

module to the Python code.

The following code uses the `NumPy`

module to initialize a two-dimensional matrix in Python.

1 2 3 4 5 |
import numpy as np x = np.zeros((3,3)) print(x) |

The above code provides the following output:

1 2 3 4 5 |
[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] |

That’s all about how to initialize matrix in Python.