Python Program to Construct n*m Matrix from List
Last Updated :
23 Jul, 2025
We are given a list we need to construct a n*m matrix from that list. For example, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] we need to construct a 3*4 matrix so that resultant output becomes [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] .
Using List Comprehension
This method uses list comprehension to group elements into sub-lists of length m
to form the matrix.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
n, m = 3, 4
#This list comprehension divides the list 'a' into sublists of length 'm'.
m = [a[i:i + m] for i in range(0, n * m, m)]
print(m)
Output[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Explanation:
- List comprehension creates sublists of length m (4) by iterating over the range from 0 to n * m (12) with a step size of m (4).
- Resulting list m will contain 3 sublists, each consisting of 4 elements from the original list a
Using numpy
We can create an n*m matrix using NumPy by reshaping a 1D array into a 2D array with the desired dimensions. The numpy.reshape() function enables us to transform the list into a matrix with n rows and m columns, while keeping the total number of elements unchanged.
Python
import numpy as np
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
n, m = 3, 4
# Convert the list 'a' into a NumPy array, then reshape it.
m = np.array(a).reshape(n, m)
print(m)
Output[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Explanation:
- np.array(a) converts the list a into a NumPy array.
- .reshape(n, m) reshapes the array into a matrix with n rows and m columns, where n * m should match the total number of elements in a.
Using a For Loop
By using a for loop, a list can be split into sublists of size m by iterating through the list in steps of m and appending each slice to a new list. This approach manually creates a matrix by grouping elements into rows of size m.
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
n, m = 3, 4
mat = []
for i in range(n):
# Slice the list 'a' into sublists of size 'm' and append to 'matrix'
mat.append(a[i*m : (i+1)*m])
print(mat)
Output[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Explanation:
- Code iterates n times (3 times) slicing the list a into sublists of length m (4 elements each) during each iteration using a[i*m : (i+1)*m].
- Each sliced sublist (representing a row of the matrix) is appended to the list mat creating a 3x4 matrix.
Using zip()
and iter()
This approach groups elements using zip and iter
Python
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
n, m = 3, 4
# Create a list 'mat' by grouping elements of 'a' into sublists of size 'm'
mat = [list(x) for x in zip(*[iter(a)] * m)]
print(mat)
Output[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Explanation:
- zip(*[iter(a)] * m) creates an iterator over the list a, grouping elements into tuples of size m by using the iter() function to create an iterator and then zipping them together.
- Each tuple of size m is converted into a list and added to mat, resulting in a 3x4 matrix (3 rows and 4 columns).
Similar Reads
Python Program to Convert Tuple Matrix to Tuple List Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]
8 min read
Python Program to Convert Tuple Matrix to Tuple List Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]
8 min read
Python program to Convert a Matrix to Sparse Matrix Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python program to Convert a Matrix to Sparse Matrix Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python program to Convert a Matrix to Sparse Matrix Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python Program to Convert Matrix to String Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string.Using List ComprehensionList comprehension provides a con
2 min read