GeeksforGeeks App
Open App
Browser
Continue

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 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))`

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)
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))`

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) 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))`

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]]```

Method 4: Using numpy module

Here are the steps to implement this approach:

1. Import numpy module.
2. Convert the given list into numpy array.
3. Use numpy.delete() method to delete elements from each row using row index.
4. Convert the resulting numpy array back into list.
5. 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

1. Initialize the 2D list test_list.
2. Use a for loop and the range() function to iterate over the rows of the test_list.
3. 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.
4. 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))`

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)
Auxiliary space: O(1), since the list is modified in-place without creating any additional lists.

My Personal Notes arrow_drop_up