# Create matrix in Python In the world of programming, matrices are represented as 2-D arrays used for data representation in rows and columns. These are used in various mathematical and statistical operations and they retain their shape. A matrix with an equal number of rows and columns is called a square matrix.

To create matrix in Python, we can use the `numpy` arrays or lists. We will discuss how to create matrix in Python in this article.

## Using lists to create matrix in Python

A list also stores a collection of elements at particular positions. A list can store elements of different types under a single list and mathematical operations cannot be applied directly to a list.

We can use it to create matrix in Python.

### Using the nested lists to create matrix

A nested list is a list within a list and can be used to create matrix in Python.

A very simple example is shown below.

Output:

In the above example, we created a simple nested list that represents a matrix.

Note that we did not print the list directly. This is done because that will print the list in a single line. That is why we iterate through every row individually. We can also do this more cleanly with the `join()` function.

See the code below.

Output:

The difference in using this method to print a matrix is that it joins the elements of a row into a single string before printing it.

## Using the `for` loop to create matrix

We can use the `for` loop to create matrix in Python using lists in Python. If we have a single list then we can iterate it the required number of times within the list, and it will change to a nested list acting as a matrix.

For example,

Output:

This method takes a single list and changes it to a matrix of the required shape.

There is another way to use the `for` loop to create matrix in Python.

For example,

Output:

In the above example,

• We first initialized two empty lists. One will act like a row and the other will act as the final matrix.
• We run a nested loop. The inner loop adds an element to the list `lst`.
• In the outer loop, `lst` gets appended to the matrix list `m` as a row.
• We then initialize the `lst` as an empty list and the loop runs again for the next row.

This method is very convenient if we wish to take the input from the user for every element in a list. For this, we have to simply ask for the input in the inner loop before appending the element to the `lst` list.

The final list discussed in the above examples can be directly converted to a `numpy` array using the `numpy.array()` or the `numpy.matrix()` function. These methods will be discussed below.

## Using `numpy` arrays to create matrix in Python

A `numpy` array is used to store a collection of elements of the same type. However, we can apply mathematical operations directly to an array and they can store a longer sequence of data efficiently. That is why it is considered a better approach to use `numpy` arrays for matrices in Python.

Let us discuss different methods to create matrix in Python using `numpy` arrays.

### Using the `numpy.array()` function to create matrix in Python

We can create a 2-D `numpy` array using the `numpy.array()` function in Python.

For example,

Output:

In the above example, we created a simple matrix in Python. We have verified this by displaying the shape of the matrix, which shows that it is a 2-D array with 3 columns and 3 rows.

### Using the `numpy.matrix()` function to create matrix in Python

The `numpy.matrix()` function is used to return a 2-D array. It works similarly to the `numpy.array()` function discussed earlier.

For example,

Output:

### Using the `numpy.reshape()` function to create matrix in Python

The `numpy.reshape()` can also be used to create matrix in Python. This function can be used to alter the shape of the array. We can use it to change the shape of a 1-D array to a 2-D array without changing its elements.

See the code below.

Output:

Let us discuss what happened in the above example.

• We had a 1-D array consisting of nine elements in total.
• The `reshape()` function modified this to a 2-D array with a shape of `(3,3)`.
• All the elements were arranged in the matrix with 3 rows and 3 columns.

Note that we have to be careful of the number of elements in an array before changing its shape. Since a matrix of shape `(3,3)` will have 9 elements, the above example won’t work if the total elements in the 1-D array are not 9.

### Using the `numpy.append()` function to create matrix in Python

We can use the `numpy.append()` function to create matrix in Python out of an existing array. The way we achieve this is by appending rows to an existing array.

See the code below.

Output:

In the above example,

• We had a 1-D array called `m` containing a total of three elements.
• We used the `append()` function to add two more rows to the existing array and create a new array called `new`.
• The `axis` parameter is specified as `0` in the `append()` function because we wish to add the elements as rows.
• The `new` array is a matrix with a shape of `(3,3)`.

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

## Related Posts

• 11 September

### Python add commas to number

Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

• 30 August

### Python write array to CSV

Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

• 30 August

### Multiply in Python

Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

• 30 August

### How to make auto clicker in Python

Table of ContentsUsing the pynput module to create an auto clickerUsing the pyautogui module to create an auto clickerUsing the pywin32 (winauto32api) module to create an auto clicker Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python. An auto-clicker […]

• 30 August

### Iterate through files in directory in python

Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

• 30 August

### Write variable to file in python

Table of ContentsUsing the str() functionUsing the repr() functionUsing the string formattingUsing the pickle.dump() functionUsing the numpy.savetxt function File handling is an important concept of programming. Python allows us to read and write content to an external file. In this article, we will discuss several ways to write variable to file in Python. First, let […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.