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 module
import numpy as np
# create array
arr = np.array([1, 2, 4, 5])
print("Before conversion: ", arr)
print(type(arr))
# Converting numpy to list
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 module
import numpy as np
# create array
print("\nArray:")
arr = np.array([1, 2, 4, 5])
print(arr)
print(type(arr))
# apply method
lis = arr.tolist()
# display list
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 module
import numpy as np
# create array
print("\nArray:")
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(arr)
print(type(arr))
# apply method
lis = arr.tolist()
# display list
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 module
import numpy as np
# Create NumPy array
num_array = np.array([1, 2, 3, 4, 5])
# Convert NumPy array to list using list() constructor
list_from_array = list(num_array)
# Display the result
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
# Create a NumPy array
numpy_array = np.array([1, 2, 3, 4, 5])
# Convert NumPy array to list using list comprehension
list_from_array = [element for element in numpy_array]
# Display the original array and the converted list
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
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Initialize an empty list
list_from_array = []
# Use append() to add elements to the list one by one
for element in arr:
list_from_array.append(element)
# Print the original array and the converted list
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 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? Mod
6 min read
How to Convert NumPy Matrix to Array
In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 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 a list and tuple into NumPy arrays?
In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read
How to Convert NumPy Array of Floats into Integers
In this article, we will see how to convert NumPy Array of Floats into Integers. We are given a NumPy array of float-type values. Our task is to convert all float-type values of Numpy array to their nearest array of integer values.Input: [1.2, 4.5, 9.1, 6.5, 8.9, 2.3, 1.2]Output: [1, 4, 9, 6, 8, 2,
4 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
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
Converting a NumPy array into a Pandas DataFrame makes our data easier to understand and work with by adding names to rows and columns and giving us tools to clean and organize it.In this article, we will take a look at methods to convert a numpy array to a pandas dataframe. We will be discussing tw
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