Thursday, October 9, 2025
HomeData Modelling & AIPython Program to Print matrix in snake pattern

Python Program to Print matrix in snake pattern

Given an n x n matrix .In the given matrix, you have to print the elements of the matrix in the snake pattern.

Examples: 

Input :mat[][] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50}};

Output : 10 20 30 40 45 35 25 15 27 29
37 48 50 39 33 32

Input :mat[][] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
Output : 1 2 3 6 5 4 7 8 9

Matrix printing 

Python Program to Print matrix in snake pattern

We traverse all rows. For every row, we check if it is even or odd. If even, we print from left to right else print from right to left. 

Python3




# Python 3 program to print
# matrix in snake order
M = 4
N = 4
 
 
def printf(mat):
    global M, N
 
    # Traverse through all rows
    for i in range(M):
 
        # If current row is
        # even, print from
        # left to right
        if i % 2 == 0:
            for j in range(N):
                print(str(mat[i][j]),
                      end=" ")
 
        # If current row is
        # odd, print from
        # right to left
        else:
            for j in range(N - 1, -1, -1):
                print(str(mat[i][j]),
                      end=" ")
 
 
# Driver code
mat = [[10, 20, 30, 40],
       [15, 25, 35, 45],
       [27, 29, 37, 48],
       [32, 33, 39, 50]]
 
printf(mat)
 
# This code is contributed
# by ChitraNayal


Output

10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 

Time complexity: O(n^m) ,Traversing over all the elements of the matrix, therefore N X M elements are there.
Auxiliary space : O(1)

Reversing Alternate rows Using Slicing

Python3




# Python 3 program to print
# matrix in snake order
M = 4
N = 4
 
 
def printf(mat):
    global M, N
    # Traverse through all rows
    for i in range(M):
        if i % 2 != 0:
            mat[i] = mat[i][::-1]
    for i in range(M):
        for j in range(N):
            print(mat[i][j], end=' ')
 
 
# Driver code
mat = [[10, 20, 30, 40],
       [15, 25, 35, 45],
       [27, 29, 37, 48],
       [32, 33, 39, 50]]
 
printf(mat)
 
# This code is contributed
# by vikkycirus


Output

10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 

Time Complexity: O(N x M), Traversing over all the elements of the matrix, therefore N X M elements are there.
Auxiliary Space: O(1)

Please refer complete article on Print matrix in snake pattern for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32347 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS