

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Remove First Diagonal Elements from a Square Matrix
When it is required to remove the first diagonal elements from a square matrix, the ‘enumerate’ and list comprehension is used.
Example
Below is a demonstration of the same
my_list = [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, 64, 23], [91, 11, 22, 14, 35]] print("The list is :") print(my_list) my_result = [] for index, element in enumerate(my_list): my_result.append([ele for index_1, ele in enumerate(element) if index_1 != index]) print("The resultant matrix is :") print(my_result)
Output
The list is : [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, 64, 23], [91, 11, 22, 14, 35]] The resultant matrix is : [[67, 85, 42, 11], [78, 10, 13, 0], [91, 23, 64, 23], [91, 11, 22, 35]]
Explanation
A list of list is defined and is displayed on the console.
An empty list is defined.
The list is iterated over using ‘enumerate’.
The list comprehension is used within the iteration previously.
Here, it is checked to see if the element’s index is same as index of enumerated element.
If they are not equal, it is appended to the empty list.
This is displayed as the output on the console.
- Related Questions & Answers
- How to find the mean of a square matrix elements by excluding diagonal elements in R?
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- C program to interchange the diagonal elements in given matrix
- Program to find diagonal sum of a matrix in Python
- Area of a square from diagonal length in C++
- Program to print a matrix in Diagonal Pattern.
- Program to convert given Matrix to a Diagonal Matrix in C++
- Python program to remove duplicate elements from a Doubly Linked List
- Python program to remove duplicate elements from a Circular Linked List
- Program to sort each diagonal elements in ascending order of a matrix in C++
- Spiraling the elements of a square matrix JavaScript
- Program to check diagonal matrix and scalar matrix in C++
- C# program to remove duplicate elements from a List
- Java program to remove duplicates elements from a List
Advertisements