# 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.

import_contacts

## Related Posts

• 17 January

### How to copy file to another directory in Python

Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

• 13 January

### Check if variable is String in python

Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

• 13 January

### Count number of characters in a string in python

Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

• 12 January

### Pandas apply function to column

Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

• 12 January

### How to Copy File in Python?

Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

• 11 January

### What is % in Python?

Table of ContentsWhat is % Operator in Python?The % Operator with strings in pythonThe % Operator as a placeholder for variables  in pythonThe % Operator as format specifiers in pythonConclusion In this article, we will cover what is % in Python and what are different ways to use % in Python. In python, there are […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.