Python - Custom Columns Matrix
Last Updated :
02 May, 2023
Sometimes, while working with Python lists, we can have a problem in which we need to extract certain columns from Matrix and recreate it. This kind of problem can have applications in data domains as they use Matrix as a prominent input parameter. Let's discuss certain ways in which this task can be performed.
Input : test_list = [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]], col_list = [2]
Output : [[3], [3], [9]]
Input : test_list = [[5, 4], [6, 2], [8, 3]], col_list = [1]
Output : [[4], [2], [3]]
Method #1: Using list comprehension This offers one of the ways to solve this problem. In this, we perform extraction of selective columns using nested list comprehension.
Python3
# Python3 code to demonstrate working of
# Custom Columns Matrix
# Using list comprehension
# initializing list
test_list = [[5, 4, 3, 4],
[7, 6, 3, 2],
[8, 3, 9, 10]]
# printing original list
print("The original list : " + str(test_list))
# initializing Columns list
col_list = [1, 3]
# Custom Columns Matrix
# Using list comprehension
res = [[sub[idx] for idx in col_list] for sub in test_list]
# printing result
print("Matrix after filtering : " + str(res))
Output : The original list : [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]]
Matrix after filtering : [[4, 4], [6, 2], [3, 10]]
Time complexity: O(m*n), where m is the number of rows and n is the number of columns in the input matrix.
Auxiliary space: O(k), where k is the length of the column list. This is because we are creating a new matrix to store the filtered values, which will have k columns.
Method #2: Using itemgetter() + list comprehension The combination of above functions can be used to solve this problem. In this, we perform the task of getting indices using itemgetter().
Python3
# Python3 code to demonstrate working of
# Custom Columns Matrix
# Using itemgetter() + list comprehension
from operator import itemgetter
# initializing list
test_list = [[5, 4, 3, 4],
[7, 6, 3, 2],
[8, 3, 9, 10]]
# printing original list
print("The original list : " + str(test_list))
# initializing Columns list
col_list = [1, 3]
# Custom Columns Matrix
# Using itemgetter() + list comprehension
res = [list(itemgetter(*col_list)(ele)) for ele in test_list]
# printing result
print("Matrix after filtering : " + str(res))
Output : The original list : [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]]
Matrix after filtering : [[4, 4], [6, 2], [3, 10]]
Time complexity: O(nm), where n is the number of rows in the input list and m is the number of columns. This is because the program loops through each row of the input list and then selects the specified columns using the itemgetter() function, which takes constant time.
Auxiliary space: O(nm), where n is the number of rows and m is the number of columns, since the program creates a new matrix of the same size as the input matrix to store the selected columns.
Method #3: Using a nested for loop to iterate over the elements and columns of the original list
Initializes an empty result list, then iterates over each element of the original list. For each element, a new empty row list is created. The code then iterates over the columns specified in col_list and appends the corresponding values to the row list. Once all columns have been iterated over, the row list is appended to the result list. The resulting matrix is then printed.
Python3
# Python3 code to demonstrate working of
# Custom Columns Matrix
# Using nested for loop
# initializing list
test_list = [[5, 4, 3, 4],
[7, 6, 3, 2],
[8, 3, 9, 10]]
# printing original list
print("The original list : " + str(test_list))
# initializing Columns list
col_list = [1, 3]
# Custom Columns Matrix
# Using nested for loop
res = []
for ele in test_list:
row = []
for i in col_list:
row.append(ele[i])
res.append(row)
# printing result
print("Matrix after filtering : " + str(res))
OutputThe original list : [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]]
Matrix after filtering : [[4, 4], [6, 2], [3, 10]]
Time complexity: O(n * m), where n is the number of rows and m is the number of columns in the original list.
Auxiliary space: O(n * m), as the code creates a new list res to store the filtered matrix.
Method 5: Using numpy indexing.
Python3
import numpy as np
# initializing list
test_list = [[5, 4, 3, 4],
[7, 6, 3, 2],
[8, 3, 9, 10]]
# initializing Columns list
col_list = [1, 3]
# Custom Columns Matrix using numpy indexing
arr = np.array(test_list)
res = arr[:, col_list]
# printing original list
print("The original list : " + str(test_list))
# printing result
print("Matrix after filtering : " + str(res))
Output:
The original list : [[5, 4, 3, 4], [7, 6, 3, 2], [8, 3, 9, 10]]
Matrix after filtering : [[ 4 4]
[ 6 2]
[ 3 10]]
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the number of elements in the input list, as we are creating a new numpy array to store the filtered columns.
Similar Reads
Python | Get Kth Column of Matrix
Sometimes, while working with Python Matrix, one can have a problem in which one needs to find the Kth column of Matrix. This is a very popular problem in Machine Learning Domain and having solution to this is useful. Let's discuss certain ways in which this problem can be solved. Method #1 : Using
6 min read
Python - Key Columns Dictionary from Matrix
Given a Matrix and list of keys, map each column's values with a custom list key. Input : test_list1 = [[4, 6], [8, 6]], test_list2 = ["Gfg", "Best"]Â Output : {'Gfg': [4, 8], 'Best': [6, 6]}Â Explanation : Column wise, Key values assignment.Input : test_list1 = [[4], [6]], test_list2 = ["Gfg"]Â Out
6 min read
Python | Custom length Matrix
Sometimes, we need to initialize a matrix in Python of variable length from the list containing elements. In this article, we will discuss the variable length method initialization and certain shorthands to do so. Let's discuss certain ways to perform this. Method #1: Using zip() + list comprehensio
6 min read
Summation Matrix columns - Python
The task of summing the columns of a matrix in Python involves calculating the sum of each column in a 2D list or array. For example, given the matrix a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]], the goal is to compute the sum of each column, resulting in [13, 13, 13]. Using numpy.sum()numpy.sum() is a hi
2 min read
Add custom borders to matrix in Python
Given a Matrix, the task is to write a python program to print each row having custom borders. Input : test_list = [[4, 5, 6], [1, 4, 5], [6, 9, 1], [0, 3 ,1]], bord = "|" Output : | 4 5 6 | | 1 4 5 | | 6 9 1 | | 0 3 1 | Explanation : Matrix is ended using | border as required.Input : test_list = [[
5 min read
Python - Remove front column from Matrix
Sometimes, while working with Matrix data, we can have stray element that attached at front end of each row of matrix. This can be undesired at times and wished to be removed. Letâs discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination o
6 min read
Python - Columns to Dictionary Conversion in Matrix
Given a Matrix, Convert to Dictionary with elements in 1st row being keys, and subsequent rows acting as values list. Input : test_list = [[4, 5, 7], [10, 8, 4], [19, 4, 6], [9, 3, 6]] Output : {4: [10, 19, 9], 5: [8, 4, 3], 7: [4, 6, 6]} Explanation : All columns mapped with 1st row elements. Eg. 4
3 min read
Python - Convert Matrix to Custom Tuple Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform conversion of a Python Matrix to matrix of tuples which a value attached row-wise custom from external list. This kind of problem can have applications in data domains as Matrix is integral DS that is used
6 min read
Python - Convert Matrix to Dictionary
The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i
4 min read
Python - Add custom dimension in Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to add another dimension of custom values, this kind of problem can have problem in all kinds of domains such as day-day programming and competitive programming. Let's discuss certain ways in which this task can be p
4 min read