Python Program for Program to Print Matrix in Z form Last Updated : 29 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Python3 # Python program to print a # square matrix in Z form arr = [[4, 5, 6, 8], [1, 2, 3, 1], [7, 8, 9, 4], [1, 8, 7, 5]] n = len(arr[0]) i = 0 for j in range(0, n-1): print(arr[i][j], end=" ") k = 1 for i in range(0, n): for j in range(n, 0, -1): if(j == n-k): print(arr[i][j], end=" ") break k += 1 # Print last row i = n-1 for j in range(0, n): print(arr[i][j], end=" ") # Code contributed by Mohit Gupta_OMG <(0_o)> Output4 5 6 8 3 8 1 8 7 5 Time complexity: O(n2) for a given square matrix of order n*n Auxiliary Space: O(1) Please refer complete article on Program to Print Matrix in Z form for more details! Using numpy numpy has several efficient ways to extract rows and diagonals, as illustrated below as follows:Â Python3 import numpy as np # This function shall print the given matrix in Z-form def printZform(matrix): # Converting the matrix to np array matrix = np.array(matrix) # Printing the first row, the opposite diagonal, and the last row of the matrix print(*matrix[0], *np.diag(np.fliplr(matrix))[1:-1], *matrix[-1]) # Driver Code arr = [[4, 5, 6, 8], [1, 2, 3, 1], [7, 8, 9, 4], [1, 8, 7, 5]] printZform(arr) Output4 5 6 8 3 8 1 8 7 5 Comment More infoAdvertise with us Next Article Python Program for Program to Print Matrix in Z form K kartik Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python program to print checkerboard pattern of nxn using numpy Given n, print the checkerboard pattern for a n x n matrix Checkerboard Pattern for n = 8: It consists of n * n squares of alternating 0 for white and 1 for black. We can do the same using nested for loops and some if conditions, but using Python's numpy library, we can import a 2-D matrix and get t 3 min read Array vs Matrix in R Programming The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R ar 3 min read How to create an empty matrix with NumPy in Python? In Python, an empty matrix is a matrix that has no rows and no columns. NumPy, a powerful library for numerical computing, provides various methods to create matrices with specific properties, such as uninitialized values, zeros, NaNs, or ones. Below are different ways to create an empty or predefin 3 min read Program to print a doormat pattern having a string written in the center in Python Given the number of rows R and the number of columns C, the task is to print a doormat pattern as shown in the examples below. The number of columns in the mat's design must be 3 times the number of rows in the design. The doormat should have GeeksforGeeks printed in the center. The width of the mat 2 min read numpy.matrix() in Python This class returns a matrix from a string of data or array-like object. Matrix obtained is a specialised 2D array. Syntax : numpy.matrix(data, dtype = None) : Parameters : data : data needs to be array-like or string dtype : Data type of returned array. Returns : data interpreted as a matrix Python 1 min read Like