Question:Write a program to find the sum of diagonal elements in a matrix.

**Program:**

1 2 3 4 | a = [[11,2,4],[4,5,6],[10,8,-12]] n=len(a) sum_second_diagonal=sum([a[i][j] for i in range(n) for j in range(n) if i+j==n-1]) print (sum_second_diagonal) |

**Explanation:**

Comprehensions provide a concise way to create new set of elements that satisfy a given condition from an iterable. Here the iterable is the for loop looking for diagonal elements. The list thus formed is passed to sum which returns the sum of elements in the list.

**Output:**

1 | 19 |

## Leave a Reply