Python Practice › NumPy Programs › Program 50
Reverse the row order
Write a NumPy program to swap the rows of an array (first and last).
Input
[[6, 5], [10, 9], [7, 8]]
Output
[[ 7 8] [10 9] [ 6 5]]
Approach
- Fancy indexing with a new row order.
Solution
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])a[[0, 2]] = a[[2, 0]]print(a)Output
[[ 7 8] [10 9] [ 6 5]]
Learn the concept
New to this topic? Read the lesson, then come back.