Python list is a linear data structure that can hold heterogeneous elements. Unfortunately, Python does not have a built-in array data type, but we can use the numpy library to create and modify arrays.
To create an array in Python, use the numpy library. To install numpy in your system, type the following command.
python3 -m pip install numpy
To create a numpy array, use the numpy.array[] function. To create an empty array, use the numpy empty[] function.
During programming, there will be instances when you need to convert existing lists to arrays to perform certain operations on them. In this example, we will see how to convert lists to arrays in Python.
To convert a list to array in Python, use the np.array[] method. The np.array[] is a numpy library function that takes a list as an argument and returns an array containing all the list elements.
import numpy as np elon_list = [11, 21, 19, 18, 29] elon_array = np.array[elon_list] print[elon_array] print[type[elon_array]]
Output
[11 21 19 18 29]
In this example, we defined a list, which we converted into an array using the np.array[] function and printed the array and its data type. To check variable data type in Python, use the type[] function.
Using numpy.asarray[] method to convert list to an array
The np.asarray[] is a numpy library function that takes a list as an argument converts it into an array, and returns it. As per the definition of the numpy.asarray[] function, it calls the numpy.array[] function inside itself.
So behind the scenes, np.asarray[] function calls the np.array[] function.
def asarray[a, dtype=None, order=None]: return array[a, dtype, copy=False, order=order]
The main difference between numpy.array[] and numpy.asarray[] is that the copy flag is False in the case of numpy.asarray[], and True [by default] in the case of numpy.array[].
import numpy as np elon_list = [11, 21, 19, 18, 29] elon_array = np.asarray[elon_list] print[elon_array] print[type[elon_array]]
Output
[11 21 19 18 29]
np.array vs np.asarray
The main difference between np.array[] and np.asarray[] is that np.array[] will create a duplicate of the original object and np.asarray[] will follow the changes in the original object.
For example, when a copy of the array is made using np.asarray[], the modifications made in one array would be reflected in the other array but don’t display the changes in the list from which an array is made. In the case of np.array[], this doesn’t happen.
That is it for converting the list to an array in Python.
See also
Python list to a tuple
Python list to string
Python list to dataframe
Python list to json
Python set to list
A list in Python is a linear data structure that can hold heterogeneous elements they do not require to be declared and are flexible to shrink and grow. On the other hand, an array is a data structure which can hold homogeneous elements, arrays are implemented in Python using the NumPy library. Arrays require less memory than list.
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.
In Python lists can be converted to arrays by using two methods from the NumPy library:
- Using numpy.array[]
Python3
import
numpy
lst
=
[
1
,
7
,
0
,
6
,
2
,
5
,
6
]
arr
=
numpy.array[lst]
print
[
"List: "
, lst]
print
[
"Array: "
, arr]
Output:
List: [1, 7, 0, 6, 2, 5, 6] Array: [1 7 0 6 2 5 6]
- Using numpy.asarray[]
Python3
import
numpy
lst
=
[
1
,
7
,
0
,
6
,
2
,
5
,
6
]
arr
=
numpy.asarray[lst]
print
[
"List:"
, lst]
print
[
"Array: "
, arr]
Output:
List: [1, 7, 0, 6, 2, 5, 6] Array: [1 7 0 6 2 5 6]
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. i.e :
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
import
numpy
lst
=
[
1
,
7
,
0
,
6
,
2
,
5
,
6
]
arr
=
numpy.asarray[lst]
print
[
"List:"
, lst]
print
[
"arr: "
, arr]
arr1
=
numpy.asarray[arr]
print
[
"arr1: "
, arr1]
arr1[
3
]
=
23
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.