CONVERT A LIST INTO ARRAY
Lists can be converted to arrays using the built-in functions in the Python NumPy library. NumPy provides us with two functions to convert a list into an array:
- numpy.array()
- numpy.asarray()
USING NUMPY.ARRAY()
>>> import numpy as np
>>> original_list = [1,2,3,4,5]
>>> print("original is ", end = " ")
>>> print(type(original_list))
>>> changed_array = np.array(original_list)
>>> print("\n final is ", end = " ")
>>> print(type(changed_array))
Output:
original is <class 'list'>
final is <class 'numpy.ndarray'>
USING NUMPY.ASARRAY()
>>> import numpy as np
>>> original_list = [1,2,3,4,5]
>>> print("original is ", end = " ")
>>> print(type(original_list))
>>> changed_array = np.asarray(original_list)
>>> print("\n final is ", end = " ")
>>> print(type(changed_array))
Output:
original is <class 'list'>
final is <class 'numpy.ndarray'>
The main difference between np.array() and np.asarray() is that np.array() will make a copy of the object (by default) and convert that to an array, while np.asarray() will not.
We are also available in our Instagram page. Visit us Now !!
Python Course Giveaway
Coupon code:- LEARNFORFREE
Python Programming Tutorial For The Absolute Beginner + Code
Python Programming Tutorial is Your Ultimate Guide to Master Python Programmig From Absolute Beginner to Develloping App
Thanks in support of sharing such a nice idea, post is nice
Very quickly this web site will be famous among all blogging and site-building visitors, due to it’s fastidious content
Great post.
Asking questions are genuinely pleasant thing if you are not
understanding anything fully, however this piece of writing offers
pleasant understanding even.
Hello there! I could have sworn I’ve visited this website before but after going through some of
the posts I realized it’s new to me. Anyhow, I’m certainly
pleased I came across it and I’ll be book-marking it and checking back frequently!
adreamoftrains web hosting company
It’s going to be ending of mine day, but before finish I am reading this enormous post to improve
my know-how.