Sometimes, while working with Matrix data, we can have stray elements 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 of above functionalities can be used to perform this task. In this, we run a loop for each row in matrix and remove the front elements using del.
Python3
# Python3 code to demonstrate working of # Remove Initial K column elements # Using loop + del + list slicing # initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] # printing original list print ("The original list : " + str (test_list)) # initialize K K = 2 # Remove Initial K column elements # Using loop + del + list slicing for sub in test_list: del sub[:K] # printing result print ("Matrix after removal of front elements from rows : " + str (test_list)) |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front elements from rows : [[4], [6], [1]]
Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.
Method #2 : Using list comprehension + list slicing The combination of above functions can also be used to perform this task. In this, we just iterate for each row and delete front elements using list slicing.
Python3
# Python3 code to demonstrate working of # Remove Initial K column elements # Using list comprehension + list slicing # initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] # printing original list print ("The original list : " + str (test_list)) # initialize K K = 2 # Remove Initial K column elements # Using list comprehension + list slicing res = [ele[K:] for ele in test_list] # printing result print ("Matrix after removal of front elements from rows : " + str (res)) |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front elements from rows : [[4], [6], [1]]
Time Complexity: O(n*m), where n is the number of sublists and m is the length of each sublist.
Auxiliary Space: O(1), as we’re using constant additional space
Method #3 : Using numpy
Note: Install numpy module using command “pip install numpy”
An unique approach to remove initial K column elements from a matrix is by using numpy library.
Python3
import numpy as np # Initialize matrix test_matrix = np.array([[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]) # Initialize K K = 2 # Remove Initial K column elements using numpy slicing res = test_matrix[:, K:] # Print result print ( "Matrix after removal of front elements from rows : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
Output:
Matrix after removal of front elements from rows : [[4]
[6]
[1]]
This approach uses the numpy array slicing functionality to remove the initial K column elements from the matrix. The time complexity of this approach is O(1) as it is using slicing and the auxiliary space is O(n) where n is the size of the matrix.
Method #4 : Using pandas
Note: Install numpy module using command “pip install pandas”
An unique approach to remove initial K column elements from a matrix is by using pandas library.
This approach uses the pandas to removes the first K elements from each row of a matrix. It first converts the list of lists to a pandas DataFrame and selects the columns from K onwards. Then it converts the DataFrame back to a list of lists and prints the result.
Python3
import pandas as pd #initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] #convert list to pandas DataFrame df = pd.DataFrame(test_list) #initialize K K = 2 #remove initial K column elements df = df.iloc[:, K:] #convert back to list res = df.values.tolist() #print result print ( "Matrix after removal of front elements from rows : " + str (res)) |
Output
Matrix after removal of front elements from rows : [[4], [6], [1]]
Time Complexity: O(nm) where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(nm) as a new matrix is created to store the updated matrix after removing the initial K columns.