## 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 array that is considering is multi-dimensional whereas a list that contains array elements will be returned if the array is one-dimensional. The method tolist() is considered as the easiest method to convert array to list and it does not permit any argument. In python, there are various libraries present to perform several functions and NumPy is one such python library that is used for array functionalities. Moreover, much linear algebra, matrix operations, and Fourier transform operations can also be used using this library.

### 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.

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. If the array has to be converted to list, use the function tolist() in numpy as mentioned below.

`arr1.tolist()`

**Notes:**The input array may be reconstructed using a = np.array(a.tolist()), even though this may lose the precision at times.

### 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 1-dimensional string array to 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, first, import the numpy library 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 list using the tolist() method and print it using the print() method.

#### Example #2

Python Program that demonstrates the tolist function by converting 2-dimensional string array to 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 also, first, import the numpy library 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 list using the tolist() method.

#### Example #3

Python Program that demonstrates the tolist function by converting a 2-dimensional numerical array to 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, first, import the numpy library 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 list using the tolist() method. On executing the code, a list that consists of array elements gets printed as shown in the result.

#### Example #4

Python Program that demonstrates the tolist function by converting a 1-dimensional numerical array to 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 an 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 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 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 also, import the numpy library and set 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 list using tolist() method.

### Conclusion

numpy ndarray tolist() is a function that converts the array to a list. The method tolist() is considered as the easiest method to convert array to list and it does not permit any argument. 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 –

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses