In this Python tutorial, we will learn the **Python shape of an array**, and also we will cover these topics:

- Python shape of a 2D array
- Python shape of a nested array
- Python shape and type of N-d array
- Python reshape of a multidimensional array
- Python shape of a 3D array
- Python index array
- Python print the shape of a multidimensional array
- Python transpose of 1D array
- Python change the shape of a 1D array to a 3D array
- Python change the shape of an array

## Python shape of a 2D array

Here, we can see **shape of a 2D array **in python.

- In this example, I have imported a module called
**numpy as np.**The**NumPy**library is used to work with an array. - I have taken a variable as an
**array**and I have assigned an array as**array = np.array([[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]])**. - And I have used
**np.array**to get the dimensions of an array, the shape property is used to get the current shape of an array. to get the output I have to**print(array.shape)**.

Example:

```
import numpy as np
array = np.array([[1, 2, 3, 4, 5], [5, 6, 7, 8, 9]])
print(array.shape)
```

We can see the output (2, 5) because there is 2 array which consists of 5 elements in it, which is the shape of an array. You can refer to the below screenshot for the output.

## Python shape of a nested array

Here, we can see** how to find the** **shape of a nested array** in python.

- In this example, I have imported a module called
**numpy**as**np**. The**NumPy**library is used to work with an array and created a variable called an array. - The variable array is assigned as
**array = np.zeros((5,3))**, the np.zeros are used to get an array of a given size and shape filled with**zeros.** - The ((5,3)) is the size and shape of an array, to get the output I have used
**print(array)**.

Example:

```
import numpy as np
array = np.zeros((5,3))
print(array)
```

In the below screenshot, you can see the array with size of (5,3) with zeros.

## Python shape and type of N-d array

Now we can see **how to find the shape and type of N-d array** in python.

- In this example, I have imported a module called
**numpy**as**np.**The**NumPy**library is used to work with an array. - Taken a variable as x and assigned an array as
**x = np.array([[1, 2, 4],[3, 4, 5],[4, 5, 6]])**. - The
**np.array**is used to find the dimension of an array, and variable**a = x.shape**is used to find the shape of an array and variable**b = x.dtype**is used to find the type of an array. - To get the output as a shape I have used
**print(a)**and to get the output as a datatype, I have used**print(b)**.

Example:

```
import numpy as np
x = np.array([[1, 2, 4],[3, 4, 5],[4, 5, 6]])
a = x.shape
b = x.dtype
print(a)
print(b)
```

In the below screenshot, you can see the shape and datatype of an array as the output.

## Python reshape of a multidimensional array

Now, we can see **how to reshape of a multidimensional array **in python.

- In this example, I have imported a module called
**numpy as np**. The**NumPy**library is used to work with an array. - And assigned a variable x as
**x = np.array([[ 0, 1, 3, 4],[ 4, 5, 6, 7],[ 8, 9, 10, 11],[12,13,14,15,]])**. - The
**np.array**is used to find the dimension of an array, and for the variable**array,**I have assigned it as**array = x.reshape(4, 2, 2).** - The
**reshape()**function is used to give a new shape for an array without changing the data. - The (4,2,2) 4 is no of elements in the array
**2 is the row**and another**2 is a column,**to get the output I have used**print(array)**.

Example:

```
import numpy as np
x = np.array([[ 0, 1, 3, 4],[ 4, 5, 6, 7],[ 8, 9, 10, 11],[12,13,14,15,]])
array = x.reshape(4, 2 , 2)
print(array)
```

In the below screenshot, you can see the output as reshaped array.

## Python shape of a 3D array

Here, we can see **how to get the shape of a 3D array** in python.

- In this example, I have imported a module called
**numpy as np**. The**NumPy**library is used to work with an array. - And assigned a variable
**array_3d**as**array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])**. - The
**np. array**is used to get the dimension of the array, to get the output I have used**print(array_3d.shape)**.

Example

```
import numpy as np
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
print(array_3d.shape)
```

We can see the shape 3d array as the output in the below screenshot.

## Python index array

Here, we can see** index array **in python.

- In this example, I have imported a module called
**numpy as np**. The**NumPy**library is used to work with an array. - And assigned a variable array as
**array = np.arange(20,2,-2)**. - The
**np.arange**is an inbuilt numpy function that returns a ndarray object containing a spaced value with a defined interval. - The
**(20,2,-2)**is the range given between 20 to 2 with the difference value -2. - To get the output I have used
**print(array)**.

Example:

```
import numpy as np
array = np.arange(20,2,-2)
print(array)
```

We can see the output as the array from range 20 to 2 with 2 difference value between them. You can refer to the below screenshot for the output.

## Python print shape of a multidimensional array

Here, we can **how to print the shape of a multidimensional array** in python.

- In this example, I have imported a module called
**numpy**as**np.**The**NumPy**library is used to work with an array. - I have taken three variables as
**array1 = np.array([[1,2]])**, and**array2 = np.array([[1, 3], [2, 4]])**, and**array3 = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [3, 6, 9, 12]])**with all the three with different dimensions. - The
**np.array**is used to get the dimension of an array. - To get the ouput I have used
**print(array1.shape), print(array2.shape), print(array3.shape)**.

Example:

```
import numpy as np
array1 = np.array([[1,2]])
array2 = np.array([[1, 3], [2, 4]])
array3 = np.array([[1, 3, 5, 7], [2, 4, 6, 8], [3, 6, 9, 12]])
print(array1.shape)
print(array2.shape)
print(array3.shape)
```

In the below screenshot, you can see the shape of multi dimensional array as the output.

## Python transpose of 1D array

Now, we can see** how to transpose 1D array **in python.

- In this example, I have imported a module called
**numpy as np**. The**NumPy**library is used to work with an array. - The variable array is assigned as
**array = np.arange(4).** - The
**np.arange**is an inbuilt numpy function that returns a ndarray object containing a spaced value with a defined interval 4 is the range of an array. - To transpose an array I have used the
**transpose()**function. To get the output I have used**print(array.transpose())**.

Example:

```
import numpy as np
array = np.arange(4)
print(array.transpose())
```

You can see the array of range 4 as the output. You can refer to the below screenshot for the output.

## Python change shape of a 1D array to a 3D array

Now, we can see** how to change the shape of a 1D array to a 3D array** in python.

- In this example, I have imported a module called
**numpy as np**and assigned, the variable array as**array = np.array([2,4,6,8,10,12,14,16,18,20,22,24])**. The**np.array**is used to get the dimension of an array. - To change the shape of an array I have created another variable as
**array_3d**and assigned**array_3d = array.reshape(2, 3, 2)**. - The
**reshape()**function is used to get the new shape for an array without changing the data. To get the output I have used**print(array_3d).**

Example:

```
import numpy as np
array = np.array([2,4,6,8,10,12,14,16,18,20,22,24])
array_3d = array.reshape(2, 3, 2)
print(array_3d)
```

The input array was of 1d array and in the output you can see 3d array. You can refer to the below screenshot for the output.

## Python change shape of an array

Now, we can see** how to change the shape of an array** in python.

- In this example, I have imported a module called
**numpy as np**and assigned, the variable array as**array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])**. - The
**np.array**is used to get the dimension of an array. - To change the shape of an array I have created another variable as
**newarray**and assigned**newarray = array.reshape(3, 4)**. Here 3 is the number of rows and 4 is the number of columns. - The
**reshape()**function is used to get the new shape for an array without changing the data. To get the output, I have used**print(newarray).**

Example:

```
import numpy as np
array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarray = array.reshape(3, 4)
print(newarray)
```

The given array is changed to the array of range (3,4). You can refer to the below screenshot for the output.

You may like the following Python tutorials:

- How to Create a Snake game in Python using Turtle
- Python list comprehension using if-else
- Draw colored filled shapes using Python Turtle
- Python read a binary file (Examples)
- Python ask for user input
- How to convert a String to DateTime in Python
- Python Threading and Multithreading
- Python Tkinter Window Size

In this tutorial, we have learned about the **Python shape of an array**, and also we have covered these topics:

- Python shape of a 2D array
- Python shape of a nested array
- Python shape and type of N-d array
- Python reshape of a multidimensional array
- Python shape of a 3D array
- Python index array
- Python print the shape of a multidimensional array
- Python transpose of 1D array
- Python change the shape of a 1D array to a 3D array
- Python change the shape of an array

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.