Numpy array to list : In this tutorial, we will learn how to perform the operations on the array and the conversion of Numpy array to list. The Numpy library is used for the scientific calculation in a python programming language. The python language is used for the application of data analysis. It will use the ecosystem of the data-centric python package. The pandas are package used for importing and analysis of data. The Index.tolist () function used to return the list values. To covert the array to list we can use the tolist () method of Numpy module and the code is written. The tolist () converts the array to list and print the new list which is created.
Numpy uses:-
The method tolist () if the numpy.ndarry will return the list.
It will depend on the number of dimensions of numpy.ndarray then nested list is generated.
Each element is accessed by the repetition of index[n].
import numpy as np
arr=np.array ([2, 4, 5, 7, 9])
print(f’Numpy Array: \n {arr}’)
list1=arr.tolist ()
print(f’list :{ list1}’)
Output:-
numpy Array: [2 4 5 7 9]
list: [2, 4, 5, 7, 9]
import numpy as np
arr=np.array ([2, 4, 5], [6, 7, 9])
print(f’Numpy Array: \n {arr}’)
list1=arr.tolist ()
print(f’list :{ list1}’)
Output:-
numpy Array: [2 4 5] [6 7 9]
list: [[2, 4, 5], [6, 7, 9]]
The CSV file is gained by exporting array by using savetxt () method of numpy.
Example:-
import numpy
a=numpy.array ([1, 2, 3, 4, 5])
numpy.savetxt (“My array.csv”, a)
Output:-
A |
B |
C |
1.00E |
|
|
2.00E |
|
|
3.00E |
|
|
Sort the array using the sort () of the numpy module.
It will take an axis which is -1 by default and axis will specify the axis we want to sort.
-1 is the sorted array by the last axis.
Example:-
import nump0y
a=numpy.array ([2, 1, 4, 6, 1, 3])
print(“Sorted array=”, numpy.sort (a))
In this we call the sort () in print statement and a is passed to sort function.
Output:-
Sorted array= [1, 1, 2, 3, 4, 6]
Indexing is referred to the element of the array.
Example:-
import numpy
a=numpy.array ([20, 13, 23, 4, 11])
print(“Element at index 2 =”, a [2])
Output:-
Element at index 2=23
It is used by the append method in python language.
Example:-
import numpy
a = numpy.array ([11, 2, 3, 4, 5])
b = numpy.array([6, 7, 8, 9, 10])
newArray = numpy.append (a, b)
print("The new array = ", newArray)
Output:-
The new array= [11 2 3 4 5 6 7 8 9 10]