Discover the distinctions between Python lists and NumPy arrays! In this tutorial, we will understand the difference between Python List and Python Numpy array.
NumPy is the fundamental package for scientific computing in Python. Numpy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less code than is possible using Python’s built-in sequences. Numpy is not another programming language but a Python extension module. It provides fast and efficient operations on arrays of homogeneous data.
Some important points about Numpy arrays:
import numpy as np
a = np.array([1, 2, 3])
print(a)
Output:
[1 2 3]
import numpy as np
a = np.array([(1, 2, 3), (4, 5, 6)])
print(a)
Output:
[[1 2 3]
[4 5 6]]
A Python list is a collection that is ordered and changeable. In Python, lists are written with square brackets.
Some important points about Python Lists:
Below are some examples which clearly demonstrate how Numpy arrays are better than Python lists by analyzing the memory consumption, execution time comparison, and operations supported by both of them.
Here we are creating Python List using []
Var = ["Codequs", "for", "Codequs"]
print(Var)
Output:
["Codequs", "for", "Codequs"]
`Numpy Array | Python List |
It is the core Library of python which is used for scientific computing. | The core library of python provides list. |
It can contain similar datatypes. | It Contains different types of datatypes. |
We need to Numpy Library to access Numpy Arrays. | It is built-in function of python. |
It is Homogeneous. | It is both homogeneous and heterogeneous. |
In this Element wise operation is possible. | Element wise operation is not possible on the list. |
By using numpy.array() we can create N-Dimensional array. | It is by default 1-dimensional.In some cases, we can create an N-Dimensional list. But it is a long process. |
It requires smaller memory consumption as compared to Python List. | It requires more memory as compared to Numpy Array. |
In this each item is stored in a sequential manner. | It stores item in random location of the memory. |
It is faster as compared to list. | It is slow as compared to NumPy Array. |
It also have some optimism function . | It does not have some optimism function . |
In Python, a list is a built-in data structure that can hold elements of varying data types. However, the flexibility of lists comes at the cost of memory efficiency.
Python’s NumPy library supports optimized numerical array and matrix operations.
Memory Allocation
In this example, a Python list and a Numpy array of size 1000 will be created. The size of each element and then the whole size of both containers will be calculated and a comparison will be done in terms of memory consumption.
# importing numpy package
import numpy as np
# importing system module
import sys
# declaring a list of 1000 elements
S= range(1000)
# printing size of each element of the list
print("Size of each element of list in bytes: ",sys.getsizeof(S))
# printing size of the whole list
print("Size of the whole list in bytes: ",sys.getsizeof(S)*len(S))
# declaring a Numpy array of 1000 elements
D= np.arange(1000)
# printing size of each element of the Numpy array
print("Size of each element of the Numpy array in bytes: ",D.itemsize)
# printing size of the whole Numpy array
print("Size of the whole Numpy array in bytes: ",D.size*D.itemsize)
Output:
Size of each element of list in bytes: 48
Size of the whole list in bytes: 48000
Size of each element of the Numpy array in bytes: 8
Size of the whole Numpy array in bytes: 8000
In this example, here two Python lists and two Numpy arrays will be created and each container has 1000000 elements. Multiplication of elements in both the lists and Numpy arrays respectively will be carried out and the difference in time needed for the execution for both the containers will be analyzed to determine which one takes less time to perform the operation.
# importing required packages
import numpy
import time
# size of arrays and lists
size = 1000000
# declaring lists
list1 = range(size)
list2 = range(size)
# declaring arrays
array1 = numpy.arange(size)
array2 = numpy.arange(size)
# capturing time before the multiplication of Python lists
initialTime = time.time()
# multiplying elements of both the lists and stored in another list
resultantList = [(a * b) for a, b in zip(list1, list2)]
# calculating execution time
print("Time taken by Lists to perform multiplication:",
(time.time() - initialTime),
"seconds")
# capturing time before the multiplication of Numpy arrays
initialTime = time.time()
# multiplying elements of both the Numpy arrays and stored in another Numpy array
resultantArray = array1 * array2
# calculating execution time
print("Time taken by NumPy Arrays to perform multiplication:",
(time.time() - initialTime),
"seconds")
Output:
Time taken by Lists to perform multiplication: 0.07256507873535156 seconds
Time taken by NumPy Arrays to perform multiplication: 0.006612300872802734 seconds
In this example, the incapability of the Python list to carry out a basic operation is demonstrated. A Python list and a Numpy array having the same elements will be declared and an integer will be added to increment each element of the container by that integer value without looping statements. The effect of this operation on the Numpy array and Python list will be analyzed.
# importing Numpy package
import numpy as np
# declaring a list
ls =[1, 2, 3]
# converting the list into a Numpy array
arr = np.array(ls)
try:
# adding 4 to each element of list
ls = ls + 4
except(TypeError):
print("Lists don't support list + int")
# now on array
try:
# adding 4 to each element of Numpy array
arr = arr + 4
# printing the Numpy array
print("Modified Numpy array: ",arr)
except(TypeError):
print("Numpy arrays don't support list + int")
Output:
Lists don't support list + int
Modified Numpy array: [5 6 7]
Advantages of using Numpy Arrays Over Python Lists:
#python