Create a Numpy Array 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 list into Numpy Array:

Syntax-

arr=np.array(list)


program-

#import numpy
import numpy as np

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

#given character list
l=['a','b','c','d','e']
print("List is",l)
arr=np.array(l)
print("Array is",arr)

#given hetrogeneous list
l=[1,'b',2,'d','e']
print("List is",l)
arr=np.array(l)
print("Array is",arr)


output-

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

-In this program, we have a different lists and we convert into the Numpy array.
-Items in list are seperated by (,) while items in array seperated by whitespace as shown in above output.




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!