Python Practice › NumPy Programs › Program 50

Reverse the row order

EasyNumPy Programs · 50 of 100

Write a NumPy program to swap the rows of an array (first and last).

Advertisement
Input
[[6, 5], [10, 9], [7, 8]]
Output
[[ 7  8]
 [10  9]
 [ 6  5]]

Approach

  1. Fancy indexing with a new row order.

Solution

Python
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]]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs