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

About Diwas Pandey

Highly motivated, strong drive with excellent interpersonal, communication, and team-building skills. Motivated to learn, grow and excel in Data Science, Artificial Intelligence, SEO & Digital Marketing

View all posts by Diwas Pandey →

6 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 *