
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse Every Kth Row in a Matrix using Python
In python, a matrix is a particular type of two-dimensional array in which all of the data elements must have exactly the same size. Hence, every matrix is also a two-dimensional array, but not the other way around.For many mathematical and scientific tasks, matrices are essential data structures.
In this article, we will learn a python program to reverse every Kth row in a Matrix.
Methods Used
The following are the various methods to accomplish this task ?
Using for loop and reversed() function
Using list comprehension and slicing
Using index() and reverse() functions
Using range() function
Example
Assume we have taken an input list and kth row number. We will now reverse the elements of the input kth row using the bove methods.
Input
inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]] kth_rowno = 2
Output
[[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]
In this example, the input kth row=2. So, the elements of the 2nd row of an input matrix are reversed and the resultant matrix is printed.
Method 1 Using for loop and reversed() function
In this function we are going to take help of for loop and reversed() function of python to perform the given task
Syntax
enumerate(iterable, start=0)
The enumerate() function adds a counter to an iterable and returns the enumerate object.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input matrix.
Print the input matrix.
Create another variable to store the input kth row to be reversed.
Create an empty list for storing the resultant matrix.
Use the for loop to traverse through the index, element of the input matrix with the enumerate() function.
Use the if conditional statement to check whether the index is multiple of k.
Use the reversed() function to reverse the current element and convert it into a list with the list() function.
Append that element to the resultant list using the append() function.
Else append that current element without any modification.
Print the resultant matrix after reversing the input Kth row of an input matrix.
Example
The following program returns a matrix after reversing the input Kth row of an input matrix using for loop and reversed() function -
# input matrix inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]] # printing input matrix print("Input Matrix:\n", inputMatrix) # input Kth row kth_rowno = 2 # resultant list for storing matrix resultantList = [] # traversing through index, element of the input matrix for index, element in enumerate(inputMatrix): # checking if the index is multiple of K if (index + 1) % kth_rowno == 0: # reversing the current element and converting it to a list and # then appending to the resultant list if the condition is true resultantList.append(list(reversed(element))) else: # else appending current element to resultant list without any modification resultantList.append(element) # printing the resultant matrix after reversing the given Kth row print("Matrix after reversing the elements of 2nd row:\n", resultantList)
Output
Input Matrix: [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]] Matrix after reversing the elements of 2nd row: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]
Method 2 Using list comprehension and slicing
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
Example
The following program returns a matrix after reversing the input Kth row of an input matrix using list comprehension and slicing -
# input matrix inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]] # input Kth row kth_rowno = 2 # performing in concise syntax using list comprehension resultantList = [element[::-1] if (index + 1) % kth_rowno == 0 else element for index, element in enumerate(inputMatrix)] # printing the resultant matrix after reversing the given Kth row print("Matrix after reversing the elements of 2nd row:\n", resultantList)
Output
Matrix after reversing the elements of 2nd row: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]
Method 3 Using index() and reverse() functions
In this method we are going to use combination of index() and reverse() function of python to reverse every Kth row in a matrix. Here, revers() function reverses List objects in place, which means it doesn't take up any extra space and only modifies the original list.
Syntax
list.index(element)
Index function is the position at the first occurrence of the provided value is returned by the index() function.
Example
The following program returns a matrix after reversing the input Kth row of an input matrix using index() and the reverse() functions -
# input matrix inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]] kth_rowno = 2 # resultant list for storing matrix resultantList = [] for element in inputMatrix: # checking whether the index of the current element is equal to the kth row-1(0 index) if(inputMatrix.index(element) == kth_rowno-1): # reversing that current element if the condition is true element.reverse() # appending that element to the resultant list resultantList.append(element) else: # else appending that current element to the resultant list without any modification resultantList.append(element) # printing the resultant matrix after reversing the given Kth row print("Matrix after reversing the elements of 2nd row:\n", resultantList)
Output
Matrix after reversing the elements of 2nd row: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [8, 6, 1]]
Method 4 Using the range() function
In this method we ar going to use range() function of python to traverse every Kth row in a matrix. The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number.
Example
The following program returns a matrix after reversing the input Kth row of an input matrix using the range() function -
# input matrix inputMatrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]] # input Kth row kth_rowno = 2 # traversing from kth_rowno-1 till the length with a step value as kth row for index in range(kth_rowno-1, len(inputMatrix), kth_rowno): # reversing current element, if the condition is true inputMatrix[index] = inputMatrix[index][::-1] # printing the resultant matrix after reversing the given Kth row print("Matrix after reversing the elements of 2nd row:\n", inputMatrix)
Output
Matrix after reversing the elements of 2nd row: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]
Conclusion
We covered how to Reverse Every Kth Row in a Matrix in this article. We also learned how to go through the index and elements of the iterable using the enumerate() function.