Open In App
Related Articles

Python Program to convert a list into matrix with size of each row increasing by a number

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a list and a number N, the task here is to write a python program to convert it to matrix where each row has N elements more than previous row elements from list.

Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], N = 3
Output : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10, 12], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]
Explanation : Each row has 3 elements more than previous row.
Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], N = 4
Output : [[4, 6, 8, 1], [4, 6, 8, 1, 2, 9, 0, 10], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]
Explanation : Each row has 4 elements more than previous row.

Method 1: Using loop and slicing

In this, we perform the task of getting chunks using slicing, and the conversion of the list into a Matrix is done using a loop.

Program:

Python3

# Initializing list
test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# initializing N
N = 3
 
# Empty list
res = []
 
for idx in range(0, len(test_list) // N):
 
    # Getting incremented chunks
    res.append(test_list[0: (idx + 1) * N])
 
# Printing result
print("Constructed Chunk Matrix : " + str(res))

                    

Output
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10, 12], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]

Time Complexity: O(m*n), where m is the row and n is the column of the matrix
Auxiliary Space: O(k), where k is the size of the matrix

Method 2: Using list comprehension and list slicing 

In this, we perform the task of setting values using list comprehension as a shorthand. Rest all the operations are done similarly to the above method.

Program:

Python3

# initializing list
test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing N
N = 3
 
# getting incremented chunks
# using list comprehension as shorthand
res = [test_list[0: (idx + 1) * N] for idx in range(0, len(test_list) // N)]
 
# printing result
print("Constructed Chunk Matrix : " + str(res))

                    

Output
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10, 12], [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]]

Time Complexity: O(m*n)
Auxiliary Space: O(k)

Method 3: Using the numpy library’s reshape() method.

Here’s the step-by-step approach:

  1. Import the numpy library.
     
  2. Initialize the input list and the chunk size.
     
  3. Convert the input list to a numpy array.
     
  4. Reshape the array to have N columns and as many rows as necessary.
     
  5. Convert the chunk matrix back to a Python list.
     
  6. Print the result.

Python3

import numpy as np
 
# initializing list
test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing N
N = 3
 
# convert list to numpy array
arr = np.array(test_list)
 
# reshape array into chunk matrix
chunk_matrix = arr.reshape(-1, N)
 
# convert chunk matrix back to list
res = chunk_matrix.tolist()
 
# printing result
print("Constructed Chunk Matrix : " + str(res))

                    

OUTPUT:

The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [1, 2, 9], [0, 10, 12], [3, 9, 1]]

Time complexity: O(n), where n is the size of the input list. 
Auxiliary space: O(n) for the numpy array, but it is converted back to a Python list, so the overall space complexity is also O(n).

Method 4: Using a recursive function

  1. Create a list called test_list containing 12 integers.
  2. Initialize a variable called N to 3, which will be used as the chunk size.
  3. Define a function called chunk_list 
    • In the chunk_list function, check if the length of the list is less than or equal to the chunk size. If so, return a list containing the original list.
    • If the length of the list is greater than the chunk size, slice the list from the beginning to the chunk size and append it to a new list using the + operator. Then recursively call the chunk_list function with the remaining part of the list until the sublist is less than or equal to the chunk size.
  4. Call the chunk_list function with test_list and N as arguments and assign the result to a variable called res.
  5. Print the constructed chunk matrix using the print() function, the string “Constructed Chunk Matrix: “, and the str() function to convert the result to a string.

Python3

# Initializing list
test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing N
N = 3
 
def chunk_list(lst, n):
    if len(lst) <= n:
        return [lst]
    else:
        return [lst[:n]] + chunk_list(lst[n:], n)
 
res = chunk_list(test_list, N)
 
# Printing result
print("Constructed Chunk Matrix : " + str(res))

                    

Output
The original list is : [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix : [[4, 6, 8], [1, 2, 9], [0, 10, 12], [3, 9, 1]]

Time complexity: O(N * (L/N)) = O(L), where L is the length of the original list and N is the chunk size. 
Auxiliary space: O(L), as it creates a new list for each chunk of the original list. 

Method 5: Using the itertools module

 can use the zip_longest function from the itertools module to chunk the list into sublists of size N. This method fills the last sublist with a specified value (in this case, None) if its length is less than N.

Python3

from itertools import zip_longest
 
# Initializing list
test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
 
# printing original list
print("The original list is: " + str(test_list))
 
# Initializing N
N = 3
 
 
def chunk_list(lst, n):
    return [list(filter(None, sublist)) for sublist in zip_longest(*[iter(lst)] * n)]
 
 
res = chunk_list(test_list, N)
 
# Printing result
print("Constructed Chunk Matrix: " + str(res))

                    

Output
The original list is: [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1]
Constructed Chunk Matrix: [[4, 6, 8], [1, 2, 9], [10, 12], [3, 9, 1]]

Time Complexity: O(N * M), where N is the length of the list and M is the chunk size.
Auxiliary Space: O(N), where N is the length of the list, as the result is stored in a new list.



Last Updated : 18 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads