Related Articles

# Python Program to Reverse Every Kth row in a Matrix

• Last Updated : 29 Jun, 2021

Given a Matrix, the task is to write python program to reverse every Kth row. Where, K is some span value.

Input : test_list = [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]], K = 4
Output : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [6, 3], [3, 7, 4], [2, 9]]
Explanation : Every 4th row is reversed.

Input : test_list = [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]], K = 2
Output : [[5, 3, 2], [3, 6, 8], [3, 5, 2], [6, 3], [3, 7, 4], [9, 2]]
Explanation : Every 2nd row is reversed.

Method 1 : Using reversed() and loop

In this, we iterate for each row, and if Kth row is found, reversal of it is performed using reverse().

Example:

## Python3

 `# Python3 code to demonstrate working of``# Reverse Kth rows in Matrix``# Using reversed() + loop`` ` `# initializing list``test_list ``=` `[[``5``, ``3``, ``2``], [``8``, ``6``, ``3``], [``3``, ``5``, ``2``], ``             ``[``3``, ``6``], [``3``, ``7``, ``4``], [``2``, ``9``]]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# initializing K``K ``=` `3`` ` `res ``=` `[]``for` `idx, ele ``in` `enumerate``(test_list):`` ` `    ``# checking for K multiple``    ``if` `(idx ``+` `1``) ``%` `K ``=``=` `0``:`` ` `        ``# reversing using reversed``        ``res.append(``list``(``reversed``(ele)))``    ``else``:``        ``res.append(ele)`` ` `# printing result``print``(``"After reversing every Kth row: "` `+` `str``(res))`

Output:

The original list is : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]]

After reversing every Kth row: [[5, 3, 2], [8, 6, 3], [2, 5, 3], [3, 6], [3, 7, 4], [9, 2]]

Method 2 : Using Slicing and list comprehension

In this, task of reversing is performed using string slice, and list comprehension is used as shorthand to perform task of iteration.

Example:

## Python3

 `# Python3 code to demonstrate working of``# Reverse Kth rows in Matrix``# Using Slicing + list comprehension`` ` `# initializing list``test_list ``=` `[[``5``, ``3``, ``2``], [``8``, ``6``, ``3``], [``3``, ``5``, ``2``], ``             ``[``3``, ``6``], [``3``, ``7``, ``4``], [``2``, ``9``]]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# initializing K``K ``=` `3`` ` `# using enumerate() to get index and elements.``# list comprehension to perform of iteration``res ``=` `[ele[::``-``1``] ``if` `(idx ``+` `1``) ``%` `K ``=``=` `0` `else` `ele ``for` `idx,``       ``ele ``in` `enumerate``(test_list)]`` ` `# printing result``print``(``"After reversing every Kth row: "` `+` `str``(res))`

Output:

The original list is : [[5, 3, 2], [8, 6, 3], [3, 5, 2], [3, 6], [3, 7, 4], [2, 9]]

After reversing every Kth row: [[5, 3, 2], [8, 6, 3], [2, 5, 3], [3, 6], [3, 7, 4], [9, 2]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up