Numpy tolist – Python NumPy ndarray.tolist() Function

NumPy ndarray.tolist() Function:

Numpy tolist: The ndarray.tolist() function of the NumPy module converts the ndarray(n-Dimensional arary) to python list(nested).

That means it returns a python list that contains a copy of the array contents.

The data is transformed to the closest built-in Python type that is compatible.

If the array’s dimension is 0, the nested list will not be a list at all, but a normal Python scalar, because the depth of the nested list is 0.

Syntax:

numpy.ndarray.tolist()

Parameters

This function doesn’t accept any parameters

Return value

A Python list containing array elements, which may be nested is returned.

NumPy ndarray.tolist() Function in Python

Example1

Here, the Numpy arrays are transformed to Python lists.

Approach:

  • Import numpy module using the import keyword
  • Pass some random number to the array() function of the Numpy module to create a 0-Dimensional array.
  • Store it in a variable.
  • Create an array(1-Dimensional) of some random range using the arange() function of the numpy module.
  • Store it in another variable.
  • Create an array of some random range and reshape it to some random rows and columns using the reshape() function.
  • Store it in another variable.
  • Here it creates a 2-Dimensional array.
  • Convert the above given 0-Dimensional array to list using the tolist() function
  • Convert the above given 1-Dimensional array to list using the tolist() function
  • Convert the above given 2-Dimensional array to list using the tolist() function
  • Print the above obtained 0-Dimensional list.
  • Print the above obtained 1-Dimensional list.
  • Print the above obtained 2-Dimensional list.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Pass some random number to the array() function of the Numpy module
# to create a 0-Dimensional array.
# Store it in a variable.
gvn_arry0D = np.array(40)
# Create an array(1-Dimensional) of some random range using the arange()
# function of the numpy module.
# Store it in another variable.
gvn_arry1D = np.arange(2, 7)
# Create an array of some random range and reshape it to some random rows 
# and columns using the reshape() function.
# Store it in another variable.
# Here it creates a 2-Dimensional array.
gvn_arry2D = np.arange(1, 5).reshape(2,2)

# Convert the above given 0-Dimensional array to list using the tolist() function 
Lst_0D = gvn_arry0D.tolist()
# Convert the above given 1-Dimensional array to list using the tolist() function
Lst_1D = gvn_arry1D.tolist()
# Convert the above given 2-Dimensional array to list using the tolist() function
Lst_2D = gvn_arry2D.tolist()

# Print the above obtained 0-Dimensional list
print("The above obtained 0-Dimensional list is: ", Lst_0D)
# Print the above obtained 1-Dimensional list
print("The above obtained 1-Dimensional list is:", Lst_1D)
# Print the above obtained 2-Dimensional list
print("The above obtained 2-Dimensional list is: ", Lst_2D)

Output:

The above obtained 0-Dimensional list is: 40
The above obtained 1-Dimensional list is: [2, 3, 4, 5, 6]
The above obtained 2-Dimensional list is: [[1, 2], [3, 4]]

Example2

Approach:

  • Import numpy module using the import keyword
  • Create an array of some random range using the arange() function of the numpy module.
  • Store it in a variable.
  • Here it creates a 1-Dimensional array.
  • Print the given array
  • Print the type of the given array using the type() function
  • Print the type of random element from the given array using the type() function
  • Convert the above given 1-Dimensional array to list using the tolist() function
  • Store it in another variable.
  • Print the above obtained 1-Dimensional list
  • Print the type of the above obtained 1-Dimensional list using the type() function
  • Print the type of random element from the obtained 1-Dimensional list using the type() function.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Create an array of some random range using the arange()
# function of the numpy module.
# Store it in a variable.
# Here it creates a 1-Dimensional array.
gvn_arry1D = np.arange(2, 7)

# Print the given array
print("The given array is:\n", gvn_arry1D)
# Print the type of the given array using the type() function
print("The type of the given array is:\n", type(gvn_arry1D))
# Print the type of random element from the given array using the type() function
print("The type of random element from the given array is:")
print(type(gvn_arry1D[2]))
print()

# Convert the above given 1-Dimensional array to list using the tolist() function
# Store it in another variable.
Lst_1D = gvn_arry1D.tolist()

# Print the above obtained 1-Dimensional list
print("The above obtained 1-Dimensional list is:\n", Lst_1D)
# Print the type of the above obtained 1-Dimensional list using the type() function
print("The type of the above obtained 1-Dimensional list is:")
print(type(Lst_1D))
# Print the type of random element from the obtained 1-Dimensional list
# using the type() function
print("The type of random element from the obtained 1-Dimensional list is:")
print(type(Lst_1D[2]))

Output:

The given array is:
[2 3 4 5 6]
The type of the given array is:
<class 'numpy.ndarray'>
The type of random element from the given array is:
<class 'numpy.int64'>

The above obtained 1-Dimensional list is:
[2, 3, 4, 5, 6]
The type of the above obtained 1-Dimensional list is:
<class 'list'>
The type of random element from the obtained 1-Dimensional list is:
<class 'int'>