Updated May 30, 2023
Introduction to Numpy.ndarray tolist
Numpy.ndarray tolist() is a function that converts an array to a list. A nested list will be returned if the considered array is multi-dimensional. In contrast, a list that contains array elements will be returned if the array is one-dimensional. The method to list () is considered the easiest to convert an array to a list and does not permit any argument. Python offers several libraries for various functions, and among them is NumPy, which is utilized for array operations. Additionally, NumPy is extensively employed for performing linear algebra, matrix operations, and Fourier transform operations.
Syntax
Following is the syntax of the tolist function in numpy.
ndarray.tolist( )
This function returns the input array as a list that can be nested or not based on the dimension of the input array.
The parameters of the function are NONE.
Return value: List which is nested if the array is 2-d. Else, the normal list with array elements.
How ndarray tolist function work in NumPy?
Let us see the working of NumPy tolist using an example.
Suppose there is an array, as shown below.
([[ ‘1’, ‘ 2’, ‘3’, ‘4’], [‘5’, ‘6’, ‘7’, ‘8’ ]])
Here, the array is 2-dimensional. To convert an array to a list using the tolist() function in NumPy, you can follow the approach mentioned below:
arr1.tolist()
Examples of Numpy.ndarray tolist
To understand more about the working of Numpy tolist(), let us see some sample programs.
Example #1
Python Program that demonstrates the tolist function by converting a 1-dimensional string array to a list.
Code:
# Python Program that demonstrates the tolist function by converting 1-d array to list
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with few elements
arr1 =np.array( [ 'Walk' , 'Talk', 'Call', ' Small'] )
print( " Array that is converted to list: " )
print(arr1.tolist())
Output:
In this program, import the numpy library first and set the alias name as np. After that, declare a 1-dimensional array with four elements such as [‘Walk’, ‘Talk’, ‘Call’, ‘ Small’]. Here the elements are of string type. Then, convert the 1-dimensional array to a list using the tolist() method and print it using the print() method.
Example #2
Python Program that demonstrates the tolist function by converting a 2-dimensional string array to a list.
Code:
# Python Program that demonstrates the tolist function by converting 2-d array to list
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with few elements
arr1 =np.array( [['Walk' , 'Talk', 'Call', ' Small'] , ['Smile' , 'like', 'fake', ' stop']] )
print( " Array that is converted to list: " )
print(arr1.tolist())
Output:
In this program, import the numpy library first and set the alias name as np. After that, declare a 2-dimensional array with eight elements such as [[‘Walk’ , ‘Talk’, ‘Call’, ‘ Small’], [‘Smile’ , ‘like’, ‘fake’, ‘ stop’]]. Here also, the elements are of string type. Then, convert the 2-dimensional array to a list using the tolist() method.
Example #3
Python Program that demonstrates the tolist function by converting a 2-dimensional numerical array to a list.
Code:
# Python Program that demonstrates the tolist function by converting the 2-d array to list
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with few elements
arr1 =np.array( [['1' , '2', '3', ' 4'] , ['5' , '6', '7', '8']] )
print("Array that is converted to list: ")
print(arr1.tolist())
Output:
In this program, import the numpy library first and set the alias name as np. After that, declare a 2-dimensional array with eight elements such as [[‘1’ , ‘2’, ‘3’, ‘ 4’] , [‘5’ , ‘6’, ‘7’, ‘8’]]. Here the elements are of numerical type. Then, convert the 2-dimensional array to a list using the tolist() method. A list consisting of array elements gets printed on executing the code, as shown in the result.
Example #4
Python Program that demonstrates the tolist function by converting a 1-dimensional numerical array to a list.
Code:
# Python Program that demonstrates the tolist function
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with few elements
arr1 =np.array( [['1' , '2', '3', ' 4'] ] )
print("Array that is converted to list: ")
print(arr1.tolist())
Output:
Import the numpy library and set the alias name as np. After that, declare a 1-dimensional array with four elements such as [[‘1’ , ‘2’, ‘3’, ‘ 4’]]. Here the elements are of numerical type. Then, convert the 2-dimensional array to a list using the tolist() method.
Example #5
Python Program demonstrates the tolist function by converting a 2-dimensional array with string and numerical elements to a list.
Code:
# Python Program that demonstrates the tolist function
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with few elements
arr1 =np.array( [['1' , '2', '3', ' 4'] , ['Smile' , 'like', 'fake', ' stop']] )
print("Array that is converted to list: ")
print(arr1.tolist())
Output:
In this program, import the numpy library and set the alias name as np. After that, declare a 2-dimensional array with four elements of both the numerical and string such as [[‘1’ , ‘2’, ‘3’, ‘ 4’] , [‘5’ , ‘6’, ‘7’, ‘8’]]. Then, convert the 2-dimensional array to a list using the tolist() method.
Conclusion
Numpy.ndarray tolist() is a function that converts the array to a list. The method tolist() is considered the easiest way to convert an array to a list. It does not accept any arguments. In this article, different details on numpy tolist(), such as syntax, working, and examples, will be discussed in detail.
Recommended Articles
This is a guide to Numpy.ndarray tolist. Here we discuss How the ndarray tolist function works in NumPy and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –