Open In App

Print diagonals of 2D list - Python

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of printing diagonals of a 2D list in Python involves extracting specific elements from a matrix that lie along its diagonals. There are two main diagonals in a square matrix: the left-to-right diagonal and the right-to-left diagonal. The goal is to efficiently extract these diagonal elements. For example, given a matrix a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], the task would be to print the left-to-right diagonal as [1, 5, 9] and the right-to-left diagonal as [3, 5, 7].

Using list comprehension

List comprehension is a efficient way to extract diagonals from a 2D list. It directly constructs a new list by evaluating an expression for each item in the iteration. This method is particularly efficient because it avoids the overhead of using explicit loops or function calls.

Python
a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

# Left-to-right diagonal
print([a[i][i] for i in range(len(a))]) 

# Right-to-left diagonal
print([a[i][len(a)-1-i] for i in range(len(a))])

Output
[1, 5, 9]
[3, 5, 7]

Explanation:

  • Left-to-right diagonal a[i][i] selects elements where row index equals column index.
  • Right-to-left diagonal: a[i][len(a)-1-i] selects elements where the column index is the reverse of the row index.

Using map()

map() can be used to extract diagonals from a 2D list, but it generally adds more overhead compared to list comprehension for this task. It tends to be less efficient and less readable when printing diagonals from a 2D list.

Python
a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

# Left-to-right diagonal
res = list(map(lambda i: a[i][i], range(len(a))))
print(res)

# Right-to-left diagonal
res = list(map(lambda i: a[i][len(a)-1-i], range(len(a))))
print(res)

Output
[1, 5, 9]
[3, 5, 7]

Explanation:

  • map(lambda i: a[i][i], range(len(a))) applies the lambda function to select elements where the row index equals the column index.
  • map(lambda i: a[i][len(a)-1-i], range(len(a))) applies the lambda function to select elements where the column index is the reverse of the row index.

Using for loop

For loop with append method is another efficient way to extract diagonals from a 2D list. It involves iterating over each row index, appending the diagonal elements to a list. While this method requires more lines than list comprehension, it's straightforward, making it a reliable and easy-to-understand approach.

Python
a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

# Left-to-right diagonal
res = [] 
for i in range(len(a)):
    res.append(a[i][i])
print(res)

# Right-to-left diagonal
res = [] 
for i in range(len(a)):
    res.append(a[i][len(a)-1-i])
print(res)

Output
[1, 5, 9]
[3, 5, 7]

Explanation:

  • loop for i in range(len(a)) iterates through the indices and appends elements where the row index equals the column index (a[i][i]).
  • loop for i in range(len(a)) appends elements where the column index is the reverse of the row index (a[i][len(a)-1-i]).



Practice Tags :

Similar Reads