Numpy array to list in python by R4R Team

Array In numpy:

-Array is collection of element of same type in other programming language like c, c++, but in python array can contains elements of different data type too.
-Array in python or numpy are similar as list in python.
-But using of Numpy Array is efficient way instead of using list.

Conversion of Numpy array into List:

Syntax-

l=list(array)

Example-

array is [1 2 3 4 5]
list is [1,2,3,4,5]

-Here array items are seperated by whitespace while list items are seperated by commas(,)


program-

#import numpy
import numpy as np

arr=np.array([1,2,3,4,5])
print("Array is",arr)
#convert into the list
l=list(arr)
print("List is ",l)

arr=np.array([1,2,3,4,'a','b','c'])
print("nArray is",arr)
#convert into the list
l=list(arr)
print("List is ",l)


output-

Array is [1 2 3 4 5]
List is [1, 2, 3, 4, 5]

Array is ['1' '2' '3' '4' 'a' 'b' 'c']
List is ['1', '2', '3', '4', 'a', 'b', 'c']

-In this program, we have a array and we convert it into list and display both array and list.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!