Python Program to Remove First Diagonal Elements from a Square Matrix
Last Updated :
08 May, 2023
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.
Similar Reads
Python program to remove rows with duplicate element in Matrix
Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 min read
Python - Assigning Subsequent Rows to Matrix first row elements
Given a (N + 1) * N Matrix, assign each column of 1st row of matrix, the subsequent row of Matrix. Input : test_list = [[5, 8, 10], [2, 0, 9], [5, 4, 2], [2, 3, 9]] Output : {5: [2, 0, 9], 8: [5, 4, 2], 10: [2, 3, 9]} Explanation : 5 paired with 2nd row, 8 with 3rd and 10 with 4th Input : test_list
3 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] E
6 min read
Python3 Program to Generate a matrix having sum of secondary diagonal equal to a perfect square
Given an integer N, the task is to generate a matrix of dimensions N x N using positive integers from the range [1, N] such that the sum of the secondary diagonal is a perfect square.Examples:Input: N = 3Output:1 2 32 3 13 2 1Explanation:The sum of secondary diagonal = 3 + 3 + 3 = 9(= 32).Input: N =
4 min read
Python Program for Diagonally Dominant Matrix
In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant
3 min read
Python3 Program to Inplace rotate square matrix by 90 degrees | Set 1
Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12
4 min read
Python Program to Reverse Every Kth row in a Matrix
We are given a matrix (a list of lists) and an integer K. Our task is to reverse every Kth row in the matrix. For example:Input : a = [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]], K = 4 Output : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [6, 3], [3, 7, 4], [2, 9]]Using reversed() and loopWe c
4 min read
Python program to Convert a Matrix to Sparse Matrix
Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory.Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indic
2 min read
Python | Remove last element from each row in Matrix
Sometimes, while working with Matrix data, we can have a stray element attached at rear 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 th
6 min read
Python Program to Interchange Diagonals of Matrix
Given a square matrix of order n*n, you have to interchange the elements of both diagonals. Examples : Input : matrix[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : matrix[][] = {3, 2, 1, 4, 5, 6, 9, 8, 7} Input : matrix[][] = {4, 2, 3, 1, 5, 7, 6, 8, 9, 11, 10, 12, 16, 14, 15, 13} Output : matrix[][] =
2 min read