w3resource logo
Python Exercises

Python NumPy: Create a new shape to an array without changing its data

Python NumPy: Array Object Exercise-38 with Solution

Write a Python program to create a new shape to an array without changing its data.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([1, 2, 3, 4, 5, 6])
y = np.reshape(x,(3,2))
print("Reshape 3x2:")
print(y)
z = np.reshape(x,(2,3))
print("Reshape 2x3:")
print(z)

Sample Output:

Reshape 3x2:                                                           
[[1 2]                                                                 
 [3 4]                                                                 
 [5 6]]                                                                
Reshape 2x3:                                                           
[[1 2 3]                                                               
 [4 5 6]] 

Python Code Editor:

import numpy as np
x = np.array([1, 2, 3, 4, 5, 6])
y = np.reshape(x,(3,2))
print("Reshape 3x2:")
print(y)
z = np.reshape(x,(2,3))
print("Reshape 2x3:")
print(z)

Improve this sample solution and post your code through Disqus