Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Print diagonals of 2D list

Python | Print diagonals of 2D list

Given a 2D list (with equal length of sublists), write a Python program to print both the diagonals of the given 2D list.

Examples: 

Input : [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output : Diagonal 1 - [1, 5, 9]
         Diagonal 2 - [3, 5, 7]

Input : [['a', 'b'], ['c', 'd']]
Output : Diagonal 1 - ['a', 'd']
         Diagonal 2 - ['b', 'c']

Approach #1 : Using Python xrange()
We can use one-liner list comprehension along with xrange() function. xrange() is used to iterate a certain number of times in for loops. Thus, we print the element at [i][i] position in every iteration of loop. [Works in Python2] 

Python




# Python2 program to print diagonals in 2D list
 
def printDiagonal(lst):
    # To print Primary Diagonal
    print('Diagonal 1 -'),
    print([lst[i][i] for i in xrange(len(lst))])
 
    # To print Secondary Diagonal
    print('Diagonal 2 -'),
    print([lst[i][len(lst)-1-i] for i in xrange(len(lst))])
 
     
# Driver code
lst = [[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]]
 
printDiagonal(lst)


Output

Diagonal 1 - [1, 5, 9]
Diagonal 2 - [3, 5, 7]

  
Approach #2 : Using range()
This method is similar to Approach #1. The advantage of range() is that it works for both the versions of Python i.e. Python2 and Python3. 

Python3




# Python3 program to print diagonals in 2D list
 
def printDiagonal(lst):
 
    # To print Primary Diagonal
    print('Diagonal 1 - ', end ="")
    print([lst[i][i] for i in range(len(lst))])
     
    # To print Secondary Diagonal
    print('Diagonal 2 - ', end ="")
    print([lst[i][len(lst)-i-1] for i in range(len(lst))])
     
# Driver code
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
printDiagonal(lst)


Output

Diagonal 1 - [1, 5, 9]
Diagonal 2 - [3, 5, 7]

  
Approach #3 : Using enumerate()
Python enumerate() is also an alternative to above mentioned methods. It uses two variable ‘i’ and ‘r’ two traverse through enumerate(lst) and simply return the ith element of ‘r’. 

Python3




# Python3 program to print diagonals in 2D list
 
def printDiagonal(lst):
    # To print Primary Diagonal
    print('Diagonal 1 - ', end ="")
    print([r[i] for i, r in enumerate(lst)])
     
    # To print Secondary Diagonal
    print('Diagonal 2 - ', end ="")
    print([r[~i] for i, r in enumerate(lst)])
 
     
# Driver code
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
printDiagonal(lst)


Output

Diagonal 1 - [1, 5, 9]
Diagonal 2 - [3, 5, 7]

Approach#4: Using loops

This approach defines a function called “print_diagonals_2” that takes in a matrix as an argument. It then initializes two empty lists to store the elements of the matrix’s diagonals. Using a loop, it iterates through the matrix and appends the elements of the two diagonals to their respective lists. Finally, it prints the two diagonals.

Algorithm

1. Get the length of the matrix n.
2. Initialize empty lists diagonal1 and diagonal2 for the diagonals.
3. Loop through the matrix from 0 to n-1 and append the element matrix[i][i] to diagonal1 and the element matrix[i][n-i-1] to diagonal2.
4. Print the diagonals.

Python3




matrix=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
def print_diagonals(matrix):
    # Get the length of the matrix
    n = len(matrix)
    # Initialize empty lists for the diagonals
    diagonal1 = []
    diagonal2 = []
    # Loop through the matrix to get the elements of the diagonals
    for i in range(n):
        diagonal1.append(matrix[i][i])
        diagonal2.append(matrix[i][n-i-1])
    # Print the diagonals
    return diagonal1, diagonal2
print(print_diagonals(matrix))


Output

([1, 5, 9], [3, 5, 7])

Time Complexity: O(n)

Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments