# Print diagonals of 2D list in Python

Python is a high-level programming and a versatile one that is mostly preferred by the developers and it is widely used for data analysis with the help of machine learning and data science. The feature it produces to deal with the data is immense and, in this article, we will see how to print the 2-Dimensional list diagonally.  It is one of the fundamental tasks involved and is implemented efficiently with different approaches.

## Printing diagonals of 2D list

Before diving to print the diagonal of the 2D list, we need to know how the diagonals are represented. A diagonal is composed of elements and refers to those elements in a line traversing from the top left to the bottom right elements. The diagonal elements’ basic syntax is given below,

### Syntax

[D1 0 0]
[0 D2 0]
[0 0 D3]


The diagonal for the above matrix is {D1, D2, D3}. The best way to clear the definition of major and minor diagonals is by giving the example,

[D1 0 D2]
[0 D3 0]
[D4 0 D5]


The major diagonal elements are D1, D3, D5

The minor diagonal elements are D2, D3, D4

### Approach

• Approach 1 − Using the function definition to print a major diagonal.

• Approach 2 − Using the function definition to print minor diagonal.

## Approach 1: Python Program for printing diagonals of 2D list using the function definition (Major Diagonal)

The elements are printed by defining the function and the condition is satisfied when the row element equals the column element.

### Algorithm

• Step 1 − The function diagonal is defined with one argument.

• Step 2 − Then using the len function, we can get the length of rows and columns of the diagonal matrix.

• Step 3 − Using the list comprehension, for loop, can iterate through the list of row elements and another for loop can iterate through the list of column elements.

• Step 4 − The condition to print the major diagonal elements is when the row and column elements are equal.

• Step 5 − The list of matrix elements is initialized with values and then the print function will return the diagonal elements in the defined list.

### Example

#Declaring the function with one argument as mat
def diag(mat):
#To get length using the len function of rows and columns
rows = len(mat)
cols = len(mat)
#To iterate through the row range
for a in range(rows):
# To iterate through the column range
for b in range(cols):
#Condition to check whether the row element is equal to the column element
if a==b:
print(mat[a][b], end="")
print()
#Declaring the input list of elements
mat = [[10, 0, -1],
[0, 20, -2],
[-3, 0, 30]]
#function is called to get the diagonal values
diag(mat)


### Output

10
20
30


## Approach 2: Python Program for printing diagonals of 2D list using the Iteration Approach (Minor Diagonal)

The Diagonal of the given list is printed which follows the minor diagonal and the primary condition behind it is all the columns starting from the last column up to the first column having row number equal to the total number of rows minus the current place or index of the element.

### Algorithm

• Step 1 − The Function is defined with one parameter as a matrix.

• Step 2 − The length of the row and column is fetched using the len function.

• Step 3 − The for loop will iterate through the range of row and column elements.

• Step 4 − The condition that has to be satisfied is that when the row and column elements are appended must be equal to (n-1).

• Step 5 − The print function will return the ant diagonal elements in each new line.

• Step 6 − The matrix is initialized in a separate list data structure and the function is called to print the elements.

### Example

#defining the function with one argument
def diag(mat):
#To get length using the len() function of rows and columns
rows = len(mat)
cols = len(mat)
#To iterate through the row range
for a in range(rows):
#To iterate through the column range
for b in range(cols):
#checking the condition to return the antidiagonal matrix
if (a == rows - b -1):

print(mat[a][b], end=" ")
print()
#Declaring the input list of elements
mat = [[10, 0, -1],
[0, 20, -2],
[-3, 0, 30]]
#function is called to get the diagonal values
diag(mat)


### Output

-1
20
-3


## Conclusion

Python makes the process easier with simple concepts and ease of understanding the concepts behind the operation. The approaches given use the method done to print the elements in both minor and major diagonals. The Python program to print the diagonal elements can be done easily by the iteration method within the two-dimensional array.

Updated on: 04-Sep-2023

72 Views 