Convert Python list to numpy array
In this article, you will learn how to convert a Python list to a numpy array. NumPy is the fundamental package for scientific computing with Python. It is a highly optimized library for numerical operations. The support of NumPy makes the task easier. Several libraries like OpenCV, SciPy, Matplotlib when combining with NumPy increase the number of weapons in your arsenal. This module contains a number of useful concepts such as the ability to create multidimensional array objects, perform mathematical operations, tools for integrating C/C++ and Fortran code.
A list is a sequence of indexed and ordered values like an array. It is mutable, which means we can change the order of elements in a list. A list in Python is a linear data structure that can hold heterogeneous elements. It is flexible to shrink and grow and there is no need to declare it. But, an array can hold homogeneous elements. It requires less memory space than a list. In Python, the arrays are implemented using the NumPy library.
During programming, there may be instances when you will need to change the existing lists to an array to play out a specific task on them. These are the processes to convert Python list to numpy array.
Method 1: Using numpy.array()
The array object in Python is called ndarray. The array() function is used to a NumPy ndarray object. In the given example, we have defined a list, which then converted into an array using the numpy.array() function and printed the array -
# importing numpy library
import numpy
# initilizing list
number_list =[43, 12, 19, 6, 23, 17, 31]
# converting list to array
arr = numpy.array(number_list)
# printing list
print ("List: ", number_list)
# printing array
print ("Array: ", number_list)
The above code returns the following output -
List: [43, 12, 19, 6, 23, 17, 31]
Array: [43, 12, 19, 6, 23, 17, 31]
Method2: using numpy.asarray()
Both numpy.array() and numpy.asarray() methods can convert structured data into ndarray, but the main difference is that when the data source is ndarray, array will create a copy of the object array and would not reflect changes to the original array and occupy new memory space, but asarray will not.
# importing numpy library
import numpy
# initilizing list
number_list =[11, 42, 24, 12, 9, 42, 92]
# converting list to array
arr = numpy.asarray(number_list)
# printing list
print ("List: ", number_list)
# printing array
print ("Array: ", number_list)
The above code returns the following output -
List: [11, 42, 24, 12, 9, 42, 92]
Array: [11, 42, 24, 12, 9, 42, 92]
Related Articles
Alphabetical order Python
Write a program to read two numbers and print their quotient and remainder in python
ASCII value in Python
Greatest common divisor (GCD) in Python
Python nonlocal keyword
Prime factor Python
casefold in Python
strip function in Python
Vader Sentiment Analysis Python
isalpha Python
Python YouTube Downloader Script
Python project ideas for beginners
Pandas string to datetime
Fillna Pandas Example
Lemmatization nltk
How to generate QR Code in Python using PyQRCode
OpenCV and OCR Python
PHP code to send SMS to mobile from website
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml