w3resource logo


Python Exercises

Python NumPy: Move axes of an array to new positions

Python NumPy: Array Object Exercise-52 with Solution

Write a Python program to move axes of an array to new positions. Other axes remain in their original order.

Sample Solution :-

Python Code :

import numpy as np
x = np.zeros((2, 3, 4))
print(np.moveaxis(x, 0, -1).shape)
print(np.moveaxis(x, -1, 0).shape)

Sample Output:

(3, 4, 2)                                                              
(4, 2, 3)

Python Code Editor:

Improve this sample solution and post your code through Disqus