A list in Python is a linear data structure that can hold heterogeneous elements that do not require to be declared and are flexible to shrink and grow. On the other hand, an array is a data structure that can hold homogeneous elements. Arrays are implemented in Python using the NumPy library. Arrays require less memory than lists. The similarity between an array and a list is that the elements of both array and a list can be identified by its index value.
Example
Input: [1, 7, 0, 6, 2, 5, 6] Output: [1 7 0 6 2 5 6] Explanation: Given Python List is converted into NumPy Array
Convert Python List to Numpy Arrays
In Python, lists can be converted into arrays by using two methods from the NumPy library:
- Using numpy.array()
- Using numpy.asarray()
Python List to NumPy Arrays using numpy.array()
In Python, the simplest way to convert a list to a NumPy array is by using numpy.array() function. It takes an argument and returns a NumPy array as a result. It creates a new copy in memory and returns a new array.
Python3
# importing library import numpy # initializing list lst = [ 1 , 7 , 0 , 6 , 2 , 5 , 6 ] # converting list to array arr = numpy.array(lst) # displaying list print ( "List: " , lst) # displaying array print ( "Array: " , arr) |
Output:
List: [1, 7, 0, 6, 2, 5, 6]
Array: [1 7 0 6 2 5 6]
Python List to NumPy Arrays using numpy.asarray()
In Numpy, numpy.asarray() is a function that converts input data into NumPy array. It takes an argument and returns a NumPy array. It doesn’t create a new copy in memory.
Python3
# importing library import numpy # initializing list lst = [ 1 , 7 , 0 , 6 , 2 , 5 , 6 ] # converting list to array arr = numpy.asarray(lst) # displaying list print ( "List:" , lst) # displaying array print ( "Array: " , arr) |
Output:
List: [1, 7, 0, 6, 2, 5, 6]
Array: [1 7 0 6 2 5 6]
Difference between numpy.array() and numpy.asarray()
The vital difference between the above two methods is that numpy.array() will make a duplicate of the original object and numpy.asarray() would mirror the changes in the original object. When a copy of the array is made by using numpy.asarray(), the changes made in one array would be reflected in the other array also but doesn’t show the changes in the list by which if the array is made. However, this doesn’t happen with numpy.array().
Python3
# importing library import numpy # initializing list lst = [ 1 , 7 , 0 , 6 , 2 , 5 , 6 ] # converting list to array arr = numpy.asarray(lst) # displaying list print ( "List:" , lst) # displaying array print ( "arr: " , arr) # made another array out of arr using asarray function arr1 = numpy.asarray(arr) #displaying arr1 before the changes made print ( "arr1: " , arr1) #change made in arr1 arr1[ 3 ] = 23 #displaying arr1 , arr , list after the change has been made print ( "lst: " , lst) print ( "arr: " , arr) print ( "arr1: " , arr1) |
Output :
List: [1, 7, 0, 6, 2, 5, 6]
arr: [1 7 0 6 2 5 6]
arr1: [1 7 0 6 2 5 6]
lst: [1, 7, 0, 6, 2, 5, 6]
arr: [ 1 7 0 23 2 5 6]
arr1: [ 1 7 0 23 2 5 6]
In “arr” and “arr1” the change is visible at index 3 but not in 1st.