When you want to work with data of different types, a Numpy array is not feasible. That’s where a list comes into the picture.
When you convert the Numpy array to a list, each element is created as a new Python object. This means the data is copied from the contiguous NumPy array memory to the scattered Python list memory.
During the conversion, NumPy data types (int64, float64) are converted to their corresponding Python types (e.g., int, float). If it fails to convert by default, we need to convert manually.
Here are three ways to convert a Numpy array to a Python List:
- Using tolist()
- Using list()
- Using list comprehension
Method 1: Using tolist()
The most efficient and recommended method is the tolist() method, which recursively converts the array to a nested list structure. The tolist() method does not accept any argument and easily converts one-dimensional, two-dimensional, or multi-dimensional array to a simple list or nested list.
One-dimensional array
import numpy as np numpy_arr_1d = np.array([11, 18, 19, 21, 46]) print(numpy_arr_1d) # [11 18 19 21 46] print(type(numpy_arr_1d)) # <class 'numpy.ndarray'> simple_list = numpy_arr_1d.tolist() print(simple_list) # [11, 18, 19, 21, 46] print(type(simple_list)) # <class 'list'>
Here, we defined a one-dimensional numpy array and operated on the tolist() function. The type() function is used to get the type of object, which is changed from numpy.ndarray to list.
Two-dimensional array
import numpy as np numpy_arr_2d = np.array([[11, 21], [19, 18]]) print(numpy_arr_2d) # [[11 21] # [19 18]] print(type(numpy_arr_2d)) # <class 'numpy.ndarray'> nested_list = numpy_arr_2d.tolist() print(nested_list) # [[11, 21], [19, 18]] print(type(nested_list)) # <class 'list'>
We defined a 2D numpy array and used the same tolist() function to convert it into a nested list.
Three-dimensional array
import numpy as np numpy_arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(numpy_arr_3d) # [[[1 2] # [3 4]] # [[5 6] # [7 8]]] print(type(numpy_arr_3d)) # <class 'numpy.ndarray'> deep_nested_list = numpy_arr_3d.tolist() print(deep_nested_list) # [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] print(type(deep_nested_list)) # <class 'list'>
You can see that the tolist() method can handle a multi-dimensional array and gives proper output.
Pros
- It can quickly deal with multi-dimensional arrays where you must preserve the structure.
- It is efficient, simple, and concise.
Cons
- It can become less efficient for large arrays.
Method 2: Using list()
The list() constructor directly operates on a one-dimensional numpy array and returns a list. However, this conversion has one caveat.
Since numpy arrays use their own data types like np.int64, when we convert the array to a list, the type of the elements (np.int64) is retained.
If you want the elements in the Python list to be standard Python integers (int), we can explicitly convert them during the conversion process using map() and int() functions.
import numpy as np numpy_arr_1d = np.array([11, 18, 19, 21, 46]) print(numpy_arr_1d) # [11 18 19 21 46] print(type(numpy_arr_1d)) # <class 'numpy.ndarray'> main_list = list(map(int, numpy_arr_1d)) print(main_list) # [11, 18, 19, 21, 46] print(type(main_list)) # <class 'list'>
Pros
- It is efficient for one-dimensional (1D) arrays.
Cons
- It can only operate on 1D arrays and not directly work with multidimensional arrays. If you want to work with them anyway, iterate through each sub-array and use the list() function on each.
Method 3: List Comprehension
When flattening an array, list comprehension gives more conversion process control. Our final output will be a flattened list, not a nested list.
import numpy as np numpy_arr_2d = np.array([[11, 21], [19, 18]]) print(numpy_arr_2d) # [[11 21] # [19 18]] print(type(numpy_arr_2d)) # <class 'numpy.ndarray'> main_list = [int(item) for sublist in numpy_arr_2d for item in sublist] # [11, 21, 19, 18] print(main_list) # [11, 21, 19, 18] print(type(main_list)) # <class 'list'>
Before converting, we flatten the array into a 1D array and then use the int() function and list comprehension.
The final output is a simple list with the exact elements as an input array.
Pros
- It is efficient when you want to flatten the input object.
- You can modify the object’s structure however you want, which gives you immense power.
Cons
- It is not a reader-friendly code.
- It is not as efficient as the tolist() function when it comes to multidimensional arrays.
That’s all!