Python Program to Remove First Diagonal Elements from a Square Matrix

Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal.
Examples:
Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]]
Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]]
Explanation : Removed 5, 6, 4, 3, 5 from lists, 1st diagonals.
Input : test_list = [[5, 3, 3, 2], [5, 6, 7, 8], [9, 3, 4, 6], [0, 1, 2, 3]]
Output : [[3, 3, 2], [5, 7, 8], [9, 3, 6], [0, 1, 2]]
Explanation : Removed 5, 6, 4, 3 from lists, 1st diagonals.
Method 1 : Using loop and enumerate()
In this we iterate through each row using loop, and compare index of element with row number, if found equal, the element is omitted.
Program:
Python3
# initializing listtest_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [ 9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]]# printing original listprint("The original list is : " + str(test_list))res = []for idx, ele in enumerate(test_list): # removing element whose index is equal to row index res.append([el for idxx, el in enumerate(ele) if idxx != idx])# printing resultprint("Filtered Matrix : " + str(res)) |
The original list is : [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Filtered Matrix : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2 : Using list comprehension and enumerate()
In this, we perform task of iteration using list comprehension, providing one liner solution to above method.
Program:
Python3
# initializing listtest_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [ 9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]]# printing original listprint("The original list is : " + str(test_list))# list comprehension to perform task as one linerres = [[el for idxx, el in enumerate(ele) if idxx != idx] for idx, ele in enumerate(test_list)]# printing resultprint("Filtered Matrix : " + str(res)) |
The original list is : [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Filtered Matrix : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. The list comprehension and enumerate() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #3 : Using for loop and pop() method
Python3
# Python program for removing first diagonal elements# initializing listtest_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [ 9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]]# printing original listprint("The original list is : " + str(test_list))res = []j = 0for i in test_list: i.pop(j) res.append(i) j += 1# printing resultprint("Filtered Matrix : " + str(res)) |
The original list is : [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Filtered Matrix : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]]
Method 4: Using numpy module
Here are the steps to implement this approach:
- Import numpy module.
- Convert the given list into numpy array.
- Use numpy.delete() method to delete elements from each row using row index.
- Convert the resulting numpy array back into list.
- Print the filtered list.
Python3
# import numpy moduleimport numpy as np# initializing listtest_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [ 9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]]# printing original listprint("The original list is : " + str(test_list))# convert list into numpy arrayarr = np.array(test_list)# initialize result listres = []# delete element from each row using row indexfor i in range(arr.shape[0]): # create a new array with the element at index i removed row = np.delete(arr[i], i) # append the new row to the result list res.append(row.tolist())# printing resultprint("Filtered Matrix : " + str(res)) |
Output:
The original list is : [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Filtered Matrix : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]]
Time Complexity: O(n^2), where n is the number of rows in the list.
Auxiliary Space: O(n^2), due to the conversion of list to numpy array.
Method 5 : use the del statement
- Initialize the 2D list test_list.
- Use a for loop and the range() function to iterate over the rows of the test_list.
- Use the del statement to remove the diagonal element in each row. The i index is used to remove the element at the i-th position in each row, which corresponds to the diagonal position.
- Print the final result.
Python3
# Python program for removing first diagonal elements# initializing listtest_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [ 9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]]# printing original listprint("The original list is : " + str(test_list))# using del statement to remove the diagonal elements in-placefor i in range(len(test_list)): del test_list[i][i]# printing resultprint("Filtered Matrix : " + str(test_list)) |
The original list is : [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Filtered Matrix : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]]
Time complexity: O(n^2)
Auxiliary space: O(1), since the list is modified in-place without creating any additional lists.



