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 - 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
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
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
Python Program to Efficiently compute sums of diagonals of a matrix Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix. A00 A01 A02 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal
3 min read
Python3 Program for Rotate matrix by 45 degrees Given a matrix mat[][] of size N*N, the task is to rotate the matrix by 45 degrees and print the matrix.Examples:Input: N = 6, mat[][] = {{3, 4, 5, 1, 5, 9, 5}, {6, 9, 8, 7, 2, 5, 2}, {1, 5, 9, 7, 5, 3, 2}, {4, 7, 8, 9, 3, 5, 2}, {4, 5, 2, 9, 5, 6, 2}, {4, 5, 7, 2, 9, 8, 3}}Output: 3 6 4 1 9 5 4 5 8
3 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 program to search for the minimum element occurring consecutively n times in a matrix Given a matrix containing n rows. Each row has an equal number of m elements. The task is to find elements that come consecutively n times horizontally, vertically, diagonally in the matrix. If there are multiple such elements then print the smallest element. If there is no such element then print -
4 min read