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
# importing libraryimport numpy # initilizing listlst = [1, 7, 0, 6, 2, 5, 6]# converting list to arrayarr = numpy.array(lst)# displaying listprint ("List: ", lst)# displaying arrayprint ("Array: ", arr) |
Output:
List: [1, 7, 0, 6, 2, 5, 6] Array: [1 7 0 6 2 5 6]
- Using numpy.asarray()
Python3
# importing libraryimport numpy # initilizing listlst = [1, 7, 0, 6, 2, 5, 6]# converting list to arrayarr = numpy.asarray(lst)# displaying listprint ("List:", lst)# displaying arrayprint ("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
# importing libraryimport numpy # initilizing listlst = [1, 7, 0, 6, 2, 5, 6]# converting list to arrayarr = numpy.asarray(lst)# displaying listprint ("List:", lst)# displaying arrayprint ("arr: ", arr)# made another array out of arr using asarray functionarr1 = numpy.asarray(arr)#displaying arr1 before the changes made print("arr1: " , arr1)#change made in arr1arr1[3] = 23#displaying arr1 , arr , list after the change has been madeprint("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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- How to convert a list and tuple into NumPy arrays?
- Python | Convert Numpy Arrays to Tuples
- Convert Python Nested Lists to Multidimensional NumPy Arrays
- Benefit of NumPy arrays over Python arrays
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Convert list of Celsius values into Fahrenheit using NumPy array
- Flatten A list of NumPy arrays
- Python | Numpy numpy.resize()
- Python | Numpy numpy.transpose()
- Python | Numpy numpy.ndarray.__lt__()
- Python | Numpy numpy.ndarray.__gt__()
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__ge__()
- Python | Numpy numpy.ndarray.__eq__()
- Python | Numpy numpy.ndarray.__ne__()
- Python | Numpy numpy.ndarray.__neg__()
- Python | Numpy numpy.ndarray.__pos__()
- Python | Numpy numpy.ndarray.__truediv__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : riyaaggarwaldtu


