# Python | Remove Initial K column elements

• Last Updated : 29 Dec, 2019

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 code to demonstrate working of# Remove Initial K column elements# Using loop + del + list slicing  # initialize listtest_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 slicingfor sub in test_list: del sub[:K]  # printing resultprint("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]]

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 code to demonstrate working of# Remove Initial K column elements# Using list comprehension + list slicing  # initialize listtest_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 slicingres = [ele[K:] for ele in test_list]  # printing resultprint("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]]

My Personal Notes arrow_drop_up