Convert NumPy array to list in Python
Lists and arrays are the two most basic and commonly used collection objects in Python.
They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to access it.
However, there are some significant differences. Lists are already built-in in Python, whereas for arrays, we need to import the arrays
or NumPy
module and declare the arrays before using them. Arrays also store data in memory more efficiently and are widely used for mathematical operations.
In this tutorial, we will convert a numpy array to a list.
tolist()
To convert a NumPy array to a list,
use
The NumPy array tolist()
method can convert a NumPy array to a list.
For example,
import numpy as np
oned = np.array([[1, 2, 3]])
twod = np.array([[1, 2, 3], [4, 5, 6]])
print(oned.tolist())
print(twod.tolist())
Output:
[[1, 2, 3]]
[[1, 2, 3], [4, 5, 6]]
Note that this method treats the entire array as one element. That's why when we use it with a two-dimensional array, it returns a list of lists.
To avoid this, we can use tolist()
with flatten()
or ravel()
methods, which can convert an ND array into a linear 1D array. Both methods perform the same function. The difference is that ravel()
the method returns a reference view of the array and affects the original array, while flatten()
the method works on a copy of the array. Therefore, ravel()
the function is considered faster and occupies less memory.
The following code shows the usage of these functions.
import numpy as np
oned = np.array([1, 2, 3])
twod = np.array([[1, 2, 3], [4, 5, 6]])
print(oned.flatten().tolist())
print(twod.flatten().tolist())
print(oned.ravel().tolist())
print(twod.ravel().tolist())
Output:
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
[1, 2, 3]
[1, 2, 3, 4, 5, 6]
for
Convert NumPy array to list in Python
using loop
This is just a basic approach for those who are new to programming or want to customize the final list. We iterate over an array and append each element individually to an empty list. The following code achieves this.
import numpy as np
arr = np.array([1, 2, 3])
lst = []
for x in arr:
lst.append(x)
print(lst)
Output:
[1, 2, 3]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si
Calculating Mahalanobis distance in Python
Publish Date:2025/05/05 Views:185 Category:Python
-
This tutorial will show you how to find the Mahalanobis distance between two NumPy arrays in Python. Use the function scipy.spatial.distance in the Python library cdist() to calculate the Mahalanobis distance The Mahalanobis distance is a m
Implementing the ReLU function in Python
Publish Date:2025/05/05 Views:177 Category:Python
-
This tutorial will discuss the Relu function and how to implement it in Python. ReLU function Relu Functions are the foundation of machine learning and are essential when using deep learning. ReLU The term is Rectified Linear Unit an acrony
Killing a Python process
Publish Date:2025/05/05 Views:152 Category:Python
-
When programming in Python, sometimes our program gets stuck in an infinite loop. In this case, we need to terminate the program manually. This article will discuss different ways to kill a Python process. Killing a Python process using a k
Get file extension in Python
Publish Date:2025/05/05 Views:63 Category:Python
-
This tutorial shows how to get the file extension from a file name in Python. os.path Extract extension from file using module in Python The Python module os.path pre-makes useful utility functions for manipulating operating system file pat
Read the first line of a file in Python
Publish Date:2025/05/05 Views:192 Category:Python
-
In Python, we have built-in functions to handle different file operations. A text file contains a sequence of strings where each line \n is terminated by a newline character. In this tutorial, we will learn how to read the first line of a t
Reading binary files in Python
Publish Date:2025/05/05 Views:119 Category:Python
-
The program or internal processor interprets the binary file. It contains bytes as content. When we read a binary file, an bytes object of type is returned. open() Read a binary file using the function in Python In Python, we have open() th
Writing bytes to a file in Python
Publish Date:2025/05/05 Views:193 Category:Python
-
In this tutorial, we will look at how to write bytes to a binary file in Python. Binary files contain strings of type bytes. When we read a binary file, an object of type bytes is returned. In Python, bytes are represented by hexadecimal nu