When working with arrays, a frequent requirement is to convert them into standard lists, while retaining the original elements. This article delves into the art of effortlessly transforming arrays into lists using Python, maintaining the integrity of their contents.
Input: array('i', [1, 3, 5, 3, 7, 1, 9, 3])
Output: [1, 3, 5, 3, 7, 1, 9, 3]
Explanation: The array with elements [1, 3, 5, 3, 7, 1, 9, 3] are converted into list with the same elements.
Input : array('k', [45, 23, 56, 12])
Output: [45, 23, 56, 12]
Explanation: The array with elements [45, 23, 56, 12] are converted into list with the same elements.
Converting one-dimensional NumPy Array to List
Below are the methods that we will cover in this article:
Method 1: Using Loop
The code you’ve provided creates a one-dimensional NumPy array using the `np.array()` function with the elements `[1, 2, 3, 4, 5]`. Then, it uses a `for` loop to iterate through each element in the NumPy array and appends each element to the `list_from_numpy` list. Finally, it prints the content of `list_from_numpy`.
Python3
import numpy as np
numpy_array = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_numpy = []
for item in numpy_array:
list_from_numpy.append(item)
print (list_from_numpy)
|
Output:
[1, 2, 3, 4, 5]
Method 2: Using tolist() function
The NumPy library is imported as `np`. A one-dimensional NumPy array is created using the `np.array()` function, containing the elements `[1, 2, 3, 4, 5]`. The `tolist()` function, a method provided by NumPy arrays, is then used to convert the NumPy array into a standard Python list, and the resulting list is stored in the variable `list_from_numpy`. Finally, the content of `list_from_numpy` is printed, displaying the converted Python list containing the same elements as the original NumPy array. This method is concise and efficient for converting NumPy arrays to lists.
Python3
import numpy as np
numpy_array = np.array([ 1 , 2 , 3 , 4 , 5 ])
list_from_numpy = numpy_array.tolist()
print (list_from_numpy)
|
Ouput:
[1, 2, 3, 4, 5]
Method 3: using array.array() function
This code converts an array of integers to a list of integers using the built-in list() function. It creates an array using the array() method from the array module, and then calls the list() function on the array to convert it to a list. Finally, it prints the resulting list.Import array module then Create an array and Convert the array to a list using the list() function now Print the resulting list
Python3
import array
arr = array.array( 'i' , [ 1 , 3 , 5 , 3 , 7 , 1 , 9 , 3 ])
lst = list (arr)
print (lst)
|
Output
[1, 3, 5, 3, 7, 1, 9, 3]
Converting multi-dimensional NumPy Array to List
Below are the methods that we will cover in this article:
Method 1: using Loop
In this code, the outer loop iterates through each row of the multi-dimensional NumPy array. Inside the outer loop, there’s an inner loop that iterates through the items in each row and appends them to a nested list. This nested list is then appended to the list_from_numpy
list. After both loops are done, you’ll have converted the multi-dimensional NumPy array to a nested Python list.
Python3
import numpy as np
numpy_array = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]])
list_from_numpy = []
for row in numpy_array:
nested_list = []
for item in row:
nested_list.append(item)
list_from_numpy.append(nested_list)
print (list_from_numpy)
|
Output:
[[1, 2, 3],[4, 5, 6],[7, 8, 9]]
Method 2: using tolist() function
The tolist()
function will convert the multi-dimensional NumPy array into a nested list structure, preserving the dimensions and values of the original array. The output of the code would be
Python3
import numpy as np
numpy_array = np.array([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]])
nested_list = numpy_array.tolist()
print (nested_list)
|
Output:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Aug, 2023
Like Article
Save Article