CONVERT LIST INTO ARRAY IN PYTHON

Python Free Tutorial
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:

  1. numpy.array()
  2. 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 Programming Tutorial For The Absolute Beginner + Code

Python Programming Tutorial is Your Ultimate Guide to Master Python Programmig From Absolute Beginner to Develloping App

About Diwas

🚀 I'm Diwas Pandey, a Computer Engineer with an unyielding passion for Artificial Intelligence, currently pursuing a Master's in Computer Science at Washington State University, USA. As a dedicated blogger at AIHUBPROJECTS.COM, I share insights into the cutting-edge developments in AI, and as a Freelancer, I leverage my technical expertise to craft innovative solutions. Join me in bridging the gap between technology and healthcare as we shape a brighter future together! 🌍🤖🔬

View all posts by Diwas →

7 Comments on “CONVERT LIST INTO ARRAY IN PYTHON”

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

Leave a Reply

Your email address will not be published. Required fields are marked *