Python | Remove Initial K column elements
Last Updated :
11 May, 2023
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))
Output : 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))
Output : 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.
Similar Reads
Python | Remove Front K elements We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 min read
Python - Remove Columns of Duplicate Elements Given a Matrix, write a Python program to remove whole column if duplicate occurs in any row. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]] Output : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]] Explanation : 1 has duplicate as next element hence 5
3 min read
Python - Remove elements at Indices in List In Python, lists store items in a specific order and each item has an index. Removing elements by index means creating a new list that excludes items at certain positions. This helps in keeping only the needed elements while discarding others based on their index. For example:Input : li= [5, 6, 3, 7
2 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 - Remove Rows for similar Kth column element Given a Matrix, remove row if similar element has occurred in row above in Kth column. Input : test_list = [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2 Output : [[3, 4, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]] Explanation : In [2, 3, 5], we already has list [3, 4, 5] having 5 at K, i
7 min read