# Convert List to Matrix & Vice Versa in Python (Examples)

Hi! This tutorial will show you how to **turn a list into a matrix and vice-versa** in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s jump into the Python code!

## Create Sample List

Here, we will create the sample Python list that will be converted to a matrix in this tutorial. So, in your Python IDE, run the line of code below to create the sample list, which contains integer values.

sample_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Now let’s confirm if sample_list has the correct data type via the type() function.

print(type(sample_list)) # <class 'list'>

## Example 1: List to Matrix | Turn List to Matrix Using List Comprehension

In this first example, we are going to use a list comprehension to return a matrix from sample_list.

n_rows = 3 n_cols = 3 matrix = [sample_list[i:i+n_cols] for i in range(0, len(sample_list), n_cols)] print(matrix) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(type(matrix)) # <class 'list'>

The list comprehension method creates a new list of sublists, with each sublist corresponding to the number of rows in the matrix. We sliced sample_list to extract a chunk of “n_cols” elements from the original list, starting at index “i” and ending at index “i+n_cols”.

The range() function is used to generate a sequence of starting indices for each row, starting at 0 and incrementing by “n_cols”.

However, when you print out the type of the object, it outputs a list class. That is because, technically, it is still a list, although it has been reshaped as a matrix.

## Example 2: List to Matrix | Turn List to Matrix Using NumPy’s array() & reshape() Functions

In this second example, we will transform the list to a matrix, using NumPy. First, though, we will need to install and import NumPy. Therefore, run the lines of code below.

# install numpy pip install numpy # import numpy import numpy as np

Next, we will use the array() and reshape() functions to convert the list to a matrix.

n_rows = 3 n_cols = 3 matrix = np.array(sample_list).reshape(n_rows, n_cols) print(matrix) #[[1 2 3] #[4 5 6] #[7 8 9]] print(type(matrix)) # <class 'numpy.ndarray'>

In the code above, we first converted the list to a NumPy array, after which we reshaped it as a matrix using the “n_rows” and “n_cols”.

Having seen how to transform a Python list into a matrix, we will now demonstrate how to turn the matrix back into a list.

## Example 1: Matrix to List | Turn Matrix to List Using Nested For Loop

In this first example, we will turn the matrix back to a list, using a nested for loop.

my_list = [] for row in matrix: for item in row: my_list.append(item) print(my_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(my_list)) # <class 'list'>

The program above loops through the matrix, returning the matrix rows, which are also then iterated over and appended to a list.

## Example 2: Matrix to List | Turn Matrix to List Using List Comprehension

In this second example, we are going to use list comprehension to transform the matrix back into a list.

my_list = [item for row in matrix for item in row] print(my_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(my_list)) # <class 'list'>

The list comprehension method works exactly like the nested for loop, only a more concise approach.

## Example 3: Matrix to List | Turn Matrix to List Using Numpy Function Chain

In this third example, we will use a chain of functions from the NumPy library to transform the matrix into a list.

my_list = np.array(matrix).flatten().tolist() print(my_list) # [1, 2, 3, 4, 5, 6, 7, 8, 9] print(type(my_list)) # <class 'list'>

In the code above, we, first of all, converted the matrix to a NumPy array, then flattened the array, and then finally turned it into a list via tolist().

So there you have it; we have demonstrated in this tutorial how to convert a list to a matrix and vice-versa in Python. I hope you found this tutorial helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to convert a list to a matrix and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list to a matrix and vice-versa in Python.

*The YouTube video will be added soon.*

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

- Convert List of Tuples to List of Lists in Python (3 Examples)
- Convert Python List to PyTorch Tensor & Vice-Versa (Examples)
- Learn Python Programming
- Convert List of Boolean to Float in Python (2 Examples)
- Access Elements in Lists within Dictionary in Python (2 Examples)

This post has shown how to **convert a list to a matrix and vice-versa in Python**. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.