In the world of Big data, Machine learning, and Artificial Intelligence, packages like Numpy and Pandas got greater importance. The Numpy (Numerical Python) is a good alternative to the lists in Python. Their nature of fast, easy to understand, simple working allows users to compute complex problems through arrays. This article talks about the creation of arrays and how to convert Numpy array to list in python as well.

A simple Numpy array

array

The array object in the Numpy is“ndarray”. You are free to use the np.array() function you can create the Numpy arrays.

First things first, import the Numpy package.

import numpy as np
#Import the Numpy package 
import numpy as np

#Creating a simple array
my_array = np.array([5,4,3,2,1])

#Prints the array along with its type 
print(my_array)
print(type(my_array))
[5 4 3 2 1]
<class 'numpy.ndarray'>

Creating an array in Python using Numpy is very easy. For this, you have to pass the list, tuple, or any other array-like objects. The nparray() function will take this input and returns the array. 

Numpy Arrays with Dimension

numpy

Yes, you heard it right. NumPy arrays have the dimensions. You can see 0D array, 1D array, 2D array, and 3D array as well.

Note: The arrays with in the arrays are called as Nested Arrays.

We will break down these dimensions and let’s see how these works.

0D Array:

The 0D arrays are scalars which contain values in it and each value is called 0D array.

#Importing the required packages 
import numpy as np
#Creating an array
my_array = np.array(25)
#Prints the array
print(my_array)
25

1D Array:

The 1D array in the Numpy python contains the 0D array in it. Let’s see how it works.

#Importing the Numpy package
import numpy as np
#Creating an array
my_array = np.array([2,4,6,8,10])
#Prints the array
print(my_array)
[ 2  4  6  8 10]

2D Array:

The 2D array in the Numpy Python contains the 1D array in it. Let’s see how it works.

#Importing the Numpy package
import numpy as np
#Creating a 2D array
my_array = np.array([[2,4,6,8,10],[1,3,5,7,9]])
#Prints the array
print(my_array)

[[ 2  4  6  8 10]
 [ 1  3  5  7  9]]

3D Array:

The 3D array in the Numpy python contains the 2D array in it. Just like the above, but the dimension varies. Let’s see how a 3D array works.

#Importing the Numpy package 
import numpy as np
#Creating an array
my_array = np.array([[[2,4,6,8,10],[1,3,5,7,9]],[[1,4,9,13,17],[1,2,3,4,5]]])
#prints the array
print(my_array)

[[[ 2  4  6  8 10]
  [ 1  3  5  7  9]]

 [[ 1  4  9 13 17]
  [ 1  2  3  4  5]]]

Nympy Array – Dimension check

As the title says, you can even check the dimension of the Numpy array. Why everyone loves Numpy for computations is, all you need is a single line of code most of the time to get your job done.

numpy creating array

Let’s check the dimensions of the above created arrays for this process.

first = np.array(25)
second = np.array([1,2,3,4,5])
Third = np.array([[1,2,3,4],[4,3,2,1]])
Fourth = np.array([[[1,2,3,4],[5,6,7,8]],[[9,8,7,6],[6,7,8,9]]])

print(first.ndim)
print(second.ndim)
print(Third.ndim)
print(Fourth.ndim)
0
1
2
3

Fantastic!!!

You just printed the dimensions of all our arrays…That’s great…You can always use ndim function to get the dimension of the array. 

High Dimension Numpy Arrays

You can easily create the higher order dimensions in the Numpy arrays. The dimension of the array is denoted by the square brackets as you saw earlier.

numpy creating array

Let’s create a higher order array to see how it works.

#Importing the Numpy package 
import numpy as np
#Creating an array with dimension 
my_array = np.array([10,20,30,40,50],ndmin=10)
#Prints the array with its type
print(my_array)
print(type(my_array))

[[[[[[[[[[10 20 30 40 50]]]]]]]]]]
<class 'numpy.ndarray'>

In the above section, you can observe that you have created an array with a dimension of 10. Just like this, you can even create higher dimension arrays using the Numpy creating an array in Python as well.

Convert Numpy Array to List in Python

The Numpy provides the function tolist() to convert an array to a list. This function returns the nested list if and only if the input array is multidimensional.

#Import the Numpy package 
import numpy as np
#Create an array
test_array = np.array([1,2,3,4,5])
#Print an array
print(test_array)
#Convert array to list
my_list = test_array.tolist()
#Print list
print(my_list)
[1 2 3 4 5]
[1, 2, 3, 4, 5]

Well, in the above output, you can see that the tolist() function converts the array to the list. Just like this using this simple function you can easily convert the Numpy array to a list.

Convert Numpy array to list in Python – Multidimensional arrays

Now, let’s take this forward and apply the same procedure to the multidimensional arrays to check its working.

#Import the Numpy package
import numpy as np
#Creates an 2D array
test_array = np.array([[1,2,3,4,5],[6,7,8,9,0]])
#Prints an array
print(test_array)
#Converts to list
my_list = test_array.tolist()
#Prints the list
print(my_list)

[[1 2 3 4 5]
 [6 7 8 9 0]]
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]]

The tolist() function will takes the input in the form of array and converts Numpy array to list in python.

Wrapping Up

Numpy is the most useful package when it comes to data analysis and computations. You can create an array and check the dimension of the array as well using the ndim function in Python.

Using the tolist() function in python, you can easily convert Numpy array to list in python as shown above.

That’s all for now. Happy Numpy!!!

More read: Numpy docs