HOWTO · Python

How to Convert List to Matrix in Python

This tutorial discusses how to convert a list to a matrix in Python.

On this page

To convert a Python list to a matrix-like structure, first choose the representation that fits the work. Python has no built-in matrix type. A nested list is a plain Python structure that can hold rows of different lengths, while a NumPy array is suited to numerical operations and normally has a regular shape and one data type. Use nested lists when you need simple row grouping without a dependency; use NumPy when you need array operations such as reshaping or element-wise arithmetic. The examples below show both options.

Choose the right representation: nested lists or NumPy arrays

The word “matrix” often means values arranged in rows and columns. In Python, you can represent that arrangement as a list containing row lists, or as a two-dimensional NumPy array. These are different representations, not interchangeable types: a nested list can have rows with different lengths, whereas a two-dimensional array has a rectangular shape. A flat list does not become a special matrix type merely by assigning it to another variable.

If your goal is only to group values into rows, nested lists keep the data in built-in Python containers. If you need numerical array operations, convert the values to a NumPy array. NumPy must be installed in the Python environment for the NumPy examples to run.

Build rows with a loop and list slicing

Use a slice width to determine how many values belong in each row. This example groups four values into two rows of two values. The loop advances by the same width as the slice, so each pass takes the next chunk.

Run this example:

values = [2, 10, 20, 200]
width = 2
rows = []
for start in range(0, len(values), width):
    rows.append(values[start:start + width])
print(rows)
[[2, 10], [20, 200]]

If the number of input values is not a multiple of width, the last slice contains the remaining values and produces a shorter final row. Choose a positive width that makes sense for the data. This approach preserves the original values as Python objects and does not require NumPy.

Convert a list with NumPy

Call np.array with the list to create a NumPy array. The example starts with a flat list, so the result is one-dimensional; converting a list alone does not make it two-dimensional. To create rows directly, pass nested lists, with each inner list representing one row.

import numpy as np

values = [12, 10, 20, 200]
array = np.array(values)
print(array)
[ 12  10  20 200]

NumPy infers an array shape from the input structure and chooses a data type that can represent its values. When the input is nested, consistently sized inner lists form a rectangular array. NumPy arrays are useful for numerical processing, but should be described as arrays rather than as Python’s built-in matrix type.

Reshape a NumPy array into rows and columns

Use reshape to arrange an array’s existing values into a requested shape. The shape dimensions must account for all input values. For instance, four values can form two rows and two columns. Reshaping changes the array’s dimensions; it does not add or remove values.

import numpy as np

values = np.array([12, 10, 20, 200])
matrix = values.reshape((2, 2))
print(matrix)
[[ 12  10]
 [ 20 200]]

Pick dimensions based on how the data should be interpreted. A shape with a different total number of positions is incompatible with the number of values, so select dimensions whose product matches the input size. The result remains a NumPy array with two dimensions.

Use np.asarray() to convert input data

Use np.asarray when a function accepts array-like input and should work with it as a NumPy array. A list is a common input, as shown here:

import numpy as np

values = [2, 10, 20, 200]
array = np.asarray(values)
print(array)
[  2  10  20 200]

For ordinary list input, this produces an array suitable for NumPy operations. np.asarray and np.array have related conversion purposes, but their parameters and copying behavior can vary by NumPy version. Avoid relying on an unqualified claim about whether either call copies data; consult the documentation for the installed version when memory sharing matters. For a list-to-array conversion, the central choice is whether you need a NumPy array at all or whether nested Python lists already meet the task.