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 list
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]]
# printing original list
print("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 result
print("Filtered Matrix : " + str(res))
OutputThe 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 list
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]]
# printing original list
print("The original list is : " + str(test_list))
# list comprehension to perform task as one liner
res = [[el for idxx, el in enumerate(ele) if idxx != idx]
for idx, ele in enumerate(test_list)]
# printing result
print("Filtered Matrix : " + str(res))
OutputThe 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 list
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]]
# printing original list
print("The original list is : " + str(test_list))
res = []
j = 0
for i in test_list:
i.pop(j)
res.append(i)
j += 1
# printing result
print("Filtered Matrix : " + str(res))
OutputThe 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 module
import numpy as np
# initializing list
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]]
# printing original list
print("The original list is : " + str(test_list))
# convert list into numpy array
arr = np.array(test_list)
# initialize result list
res = []
# delete element from each row using row index
for 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 result
print("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 list
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]]
# printing original list
print("The original list is : " + str(test_list))
# using del statement to remove the diagonal elements in-place
for i in range(len(test_list)):
del test_list[i][i]
# printing result
print("Filtered Matrix : " + str(test_list))
OutputThe 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.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice