How to convert NumPy array to list ?
Last Updated :
01 Dec, 2023
This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding.
Convert NumPy Array to List
There are various ways to convert NumPy Array to List here we are discussing some generally used methods to convert NumPy array to list:
Convert NumPy Array to List using Type Casting
Here we are creating a Numpy array using the np.array and printing the array before the conversion and after the conversion using Python typecasting to list using list() function.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 4 , 5 ])
print ( "Before conversion: " , arr)
print ( type (arr))
arr = list (arr)
print ( "\nAfter conversion: " , type (arr))
print (arr)
|
Output:
Before conversion: [1 2 4 5]
<class 'numpy.ndarray'>
After conversion: <class 'list'>
[1, 2, 4, 5]
Convert NumPy Array to List using tolist() Method
Example 1: With One Dimensional Array
Here code uses NumPy to create an array, prints the array and its type, converts the array to a Python list using the `tolist()` method, and prints the resulting list along with its type.
Python3
import numpy as np
print ( "\nArray:" )
arr = np.array([ 1 , 2 , 4 , 5 ])
print (arr)
print ( type (arr))
lis = arr.tolist()
print ( "\nList:" )
print (lis)
print ( type (lis))
|
Output:
Array:
[1 2 4 5]
<class 'numpy.ndarray'>
List:
[1, 2, 4, 5]
<class 'list'>
Example 2: With Multidimensional Array
Here The code uses NumPy to create a 2D array, prints the array and its type, converts the 2D array to a nested Python list using the `tolist()` method, and prints the resulting list along with its type.
Python3
import numpy as np
print ( "\nArray:" )
arr = np.array([[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]])
print (arr)
print ( type (arr))
lis = arr.tolist()
print ( "\nList:" )
print (lis)
print ( type (lis))
|
Output:
Array:
[[1 2 3]
[4 5 6]
[7 8 9]]
<class 'numpy.ndarray'>
List:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
<class 'list'>
Convert NumPy Array to List using list() Constructor
Here the code utilizes NumPy to create an array, then employs the list()
constructor to convert the array to a Python list. It subsequently prints both the NumPy array and the resulting list, along with their respective types, demonstrating the conversion process.
Python3
import numpy as np
num_array = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_array = list (num_array)
print ( "NumPy Array:" )
print (num_array)
print ( "Type of NumPy Array:" , type (num_array))
print ( "\nList from NumPy Array:" )
print (list_from_array)
print ( "Type of List:" , type (list_from_array))
|
Output :
NumPy Array:
[1 2 3 4 5]
Type of NumPy Array: <class 'numpy.ndarray'>
List from NumPy Array:
[1, 2, 3, 4, 5]
Type of List: <class 'list'>
Convert NumPy Array to List using list Comprehension
Here the code utilizes NumPy to create an array, then employs list comprehension to convert the array into a Python list, and finally prints both the original NumPy array and the converted list.
Python3
import numpy as np
numpy_array = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_array = [element for element in numpy_array]
print ( "NumPy Array:" , numpy_array)
print ( "List from NumPy Array:" , list_from_array)
|
Output :
NumPy Array: [1 2 3 4 5]
List from NumPy Array: [1, 2, 3, 4, 5]
Convert NumPy Array to List using append() Method
Here the code uses NumPy to create an array, then converts it to a Python list by iterating through its elements and appending them using append() method to an initially empty list. The original array and the resulting list are printed for verification.
Python3
import numpy as np
arr = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_array = []
for element in arr:
list_from_array.append(element)
print ( "Original NumPy Array:" , arr)
print ( "List Converted from NumPy Array:" , list_from_array)
|
Output :
Original NumPy Array: [1 2 3 4 5]
List Converted from NumPy Array: [1, 2, 3, 4, 5]
Conclusion
In conclusion, the process of converting a NumPy array to a list provides flexibility and compatibility within Python programming. By utilizing methods such as `tolist()` or employing iterative techniques like appending elements, developers can seamlessly transition between these two data structures. This versatility is particularly valuable in scenarios where the distinct functionalities of lists are required, allowing for efficient data manipulation and integration into various Python applications.
Similar Reads
How to convert NumPy array to list ?
This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to Convert images to NumPy array?
Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. In this article we will see How to Convert images to NumPy array? Modu
6 min read
Convert Python List to numpy Arrays
NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
How To Convert Numpy Array To Tensor?
The tf.convert_to_tensor() method from the TensorFlow library is used to convert a NumPy array into a Tensor. The distinction between a NumPy array and a tensor is that tensors, unlike NumPy arrays, are supported by accelerator memory such as the GPU, they have a faster processing speed. there are a
2 min read
How to Convert NumPy Array of Floats into Integers
In this article, we will see how to convert the NumPy Array of Floats into Integers. We are given a NumPy array of float-type values. Our task is to convert all the float-type values of the Numpy array to their nearest array of integer values. Input1: [1.2 4.5 9.1 6.5 8.9 2.3 1.2]Output1: [1 4 9 6 8
7 min read
How to convert NumPy array to dictionary in Python?
The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi
3 min read
How to Convert Pytorch tensor to Numpy array?
In this article, we are going to convert Pytorch tensor to NumPy array. Method 1: Using numpy(). Syntax: tensor_name.numpy() Example 1: Converting one-dimensional a tensor to NumPy array C/C++ Code # importing torch module import torch # import numpy module import numpy # create one dimensional tens
2 min read
Convert Numpy Array To Xarray
Xarray is a powerful Python library for working with labeled multi-dimensional arrays. In Python, NumPy provides basic data structures and APIs for working with raw ND arrays, but, in the real world, the data is more complex, in some cases, which are encoded. The data array maps to positions in spac
3 min read
Convert Numpy Array to Dataframe
Data manipulation and analysis are common tasks in the field of data science, and two powerful libraries in Python that facilitate these tasks are NumPy and Pandas. NumPy provides support for large, multi-dimensional arrays and matrices, while Pandas offers data structures like DataFrames that make
4 min read
How to convert a dictionary into a NumPy array?
It's sometimes required to convert a dictionary in Python into a NumPy array and Python provides an efficient method to perform this operation. Converting a dictionary to NumPy array results in an array holding the key-value pairs in the dictionary. Python provides numpy.array() method to convert a
3 min read