Datatiede

How to convert Python NumPy array to python list

How to convert Python NumPy array to python list
Array object is used to store multiple values, and the list object is used in Python to do a similar task to an array object. NumPy array object is used to do different types of numerical operations in Python. The multi-dimensional array can be created by using this library. NumPy library has a built-in tolist() function to convert the NumPy array to the python list. This function does not take any argument and returns the python list. If the array is one-dimensional, then the function will return a simple python list. If the array is multi-dimensional, then the array will return the nested python list. If the array's dimension is 0, then the function will return a python scalar variable instead of a list. How tolist() function can convert different types of NumPy array to python list is shown in this tutorial.

Syntax:

list ndarray.tolist()

Here, ndarray will be a NumPy array, and the return value will be any list if the ndarray is a one-dimensional or multi-dimensional array.

Convert different types of NumPy array to Python list:

The ways of converting one or more dimensional NumPy array have shown in this tutorial by using multiple examples. You can use any python supported editor to practice the following examples. The popular python editor, PyCharm, is used in this tutorial to write the tutorial script.

Example-1: Convert a one-dimensional array into a list

The following example shows how a one-dimensional array can be converted into a python list using the tolist() function. NumPy library is imported at the beginning of the script. arange() function is used in the script to create a one-dimensional NumPy array of range values. Next, the tolist() function is used to convert the NumPy array to a python list.

# Import NumPy
import numpy as np
# Create a one-dimensional NumPy array with a range of values
np_array = np.arange(5)
# Print the NumPy array
print("The values of the NumPy array: \n", np_array)
# Convert NumPy array to python list
list_obj = np_array.tolist()
# Print the python list
print("The values of the python list: \n", list_obj)

Output:

The following output will appear after executing the script. The NumPy array values have printed in the first output, where space is used to separate the array values. The python list values have printed in the second output where the comma(,) is used to separate the list elements.

Example-2: Convert a two-dimensional array into a list

The following example shows how a two-dimensional NumPy array can be converted into a python list using the tolist() function. NumPy library is imported at the beginning of the script. array() function is used here to create the two-dimensional NumPy array of numeric values that will be printed later. tolist() function is used in the script to convert the two-dimensional array to the nested python list. Next, the python list will be printed.

# Import NumPy
import numpy as np
# Create a two-dimensional NumPy array using the list
np_array = np.array([[1, 2, 3], [4, 5, 6]])
# Print the NumPy array
print("The values of the NumPy array: \n", np_array)
# Convert NumPy array to python list
list_obj = np_array.tolist()
# Print the python list
print("The values of the python list: \n," list_obj)

Output:

The following output will appear executing the script. The first output shows the values of the NumPy two-dimensional array created from two lists. The second output shows the values of the nested python list.

Example-3: Convert a three-dimensional array into a list

The following example shows how a three-dimensional array can be converted into a nested python list using a tolist(). NumPy library is imported at the beginning of the script. Next, the set_printoptions() function is used to set the precision value for the floating numbers applied in the NumPy array. rand() function is used in the script to create a three-dimensional NumPy array. This function generates the random floating number. The three-dimensional array of the floating numbers will be created by the rand() function, multiplied by 10000 to get the numeric value with 4 digits before the decimal point and 2 digits after the decimal point. The values of the three-dimensional array will be printed in the next statement. tolist() function will convert the three-dimensional array into the nested python list, and the three nested for loop is used to format the list values with 2 digits after the decimal point. The round() function is used to do the task. Next, the formatted list will be printed.

# Import NumPy
import numpy as np
# Set the precision value for floating values
np.set_printoptions(precision=2, suppress=True)
# Declare a three-dimensional array of random numbers
np_array = np.random.rand(2, 2, 2)*10000
# Print the NumPy array
print("\nThe values of the NumPy array: \n", np_array)
# Convert NumPy array to python list
list_obj = np_array.tolist()
# Round the values of the nested list
list_obj = [[[round(val3, 2) for val3 in val2] for val2 in val1] for val1 in list_obj]
# Print the python list
print("\nThe values of the python list: \n", list_obj)

Output:

The following output will appear after executing the script. The first output shows the values of the three-dimensional NumPy array. The second output shows the formatted output of the list values.

Conclusion:

Python list is a useful object for doing various operations where multiple values can be stored in a single variable that works like the numeric array of other programming languages. Different types of arrays can be generated by using the NumPy library of Python. Sometimes, it requires converting the list into NumPy array or vice-versa. How one-dimensional and the multi-dimensional array can be converted into a python list are explained in this tutorial using simple examples. I hope the readers will convert the NumPy array into a python list easily after reading this tutorial.

Parhaat pelikonsoliemulaattorit Linuxille
Tässä artikkelissa luetellaan suositut pelikonsolin emulointiohjelmistot, jotka ovat saatavana Linuxille. Emulointi on ohjelmistojen yhteensopivuusker...
Best Linux Distros for Gaming in 2021
The Linux operating system has come a long way from its original, simple, server-based look. This OS has immensely improved in recent years and has no...
Kuinka siepata ja suoratoistaa pelisessiosi Linuxissa
Aikaisemmin pelaamista pidettiin vain harrastuksena, mutta ajan myötä pelialalla tapahtui valtava kasvu tekniikan ja pelaajien määrän suhteen. Peliala...