Numpy array to list

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.

Parameters:-

  1. It returns array as ndim level nested with a list of python scalars.
  2. If ndim is 0 then the depth of the list is 0 and it will not be a list but the python scalar.
  3. For 1d array, tolist is the same as list (a).
  4. For 1d array, tolist is the same as list (a) applied same.

Numpy uses:-

  1. For sorting the array members
  2. As the arithmetical and logical operations
  3. Input and output functions.
  4. Linear algebra and statistical operations.

Converting the numpy array to list :-

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].

1) Converting Numpy 1D array to list:-

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]

2) Converting numpy Multidimensional array to list:-

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]]

A) Numpy array to CSV :-

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

 

 

 

B) Sort Numpy array:-

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]

C) Array indexing:-

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

D) Append numpy array to another:-

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]

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India