Python Lists VS Numpy Arrays - GeeksforGeeks

19 May, 2020
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:
We can create a N-dimensional array in python using numpy.array().
Array are by default Homogeneous, which means data inside an array must be of the same Datatype. (Note you can also create a structured array in python).
Element wise operation is possible.
Numpy array has the various function, methods, and variables, to ease our task of matrix computation.
Elements of an array are stored contiguously in memory. For example, all rows of a two dimensioned array must have the same number of columns. Or a three dimensioned array must have the same number of rows and columns on each card.
Representation of Numpy array:
Single Dimensional Numpy Array;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])print(a)
[1 2 3]

Multi-dimensional Numpy Array:import numpy as np  a = np.array([(1, 2, 3), (4, 5, 6)])print(a)Output:[[1 2 3]
 [4 5 6]]

import numpy as np  a = np.array([(1, 2, 3), (4, 5, 6)])print(a)
[[1 2 3]
 [4 5 6]]

Advantages of using Numpy Arrays Over Python Lists:
consumes less memory.
fast as compared to the python List.
convenient to use.
List: A list is a collection which is ordered and changeable. In Python, lists are written with square brackets.
Some important points about Python Lists:
The list can be homogeneous or heterogeneous.
Element wise operation is not possible on the list.
Python list is by default 1 dimensional. But we can create an N-Dimensional list. But then too it will be 1 D list storing another 1D list
Elements of a list need not be contiguous in memory.
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.
Example 1: Memory consumption between Numpy array and listsIn 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 the containers will be calculated and comparison will be done in terms of memory consumption.
Below is the implementation.
# importing numpy packageimport numpy as np  # importing system moduleimport sys  # declaring a list of 1000 elements S= range(1000)  # printing size of each element of the listprint("Size of each element of list in bytes: ",sys.getsizeof(S))  # printing size of the whole listprint("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 arrayprint("Size of each element of the Numpy array in bytes: ",D.itemsize)  # printing size of the whole Numpy arrayprint("Size of the whole Numpy array in bytes: ",D.size*D.itemsize)
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

Example 2: Time comparison between Numpy array and Python listsIn this example, 2 Python lists and 2 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.
Below is the implementation.
# importing required packagesimport numpyimport time   # size of arrays and listssize = 1000000     # declaring listslist1 = range(size)list2 = range(size)   # declaring arraysarray1 = numpy.arange(size)  array2 = numpy.arange(size)   # capturing time before the multiplication of Python listsinitialTime = time.time()  # multiplying  elements of both the lists and stored in another listresultantList = [(a * b) for a, b in zip(list1, list2)]   # calculating execution timeprint("Time taken by Lists to perform multiplication:",       (time.time() - initialTime),      "seconds")   # capturing time before the multiplication of Numpy arraysinitialTime = 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")
Time taken by Lists : 0.15030384063720703 seconds
Time taken by NumPy Arrays : 0.005921125411987305 seconds

Example 3: Effect of operations on Numpy array and Python ListsIn 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.
Below is the implementation.
# importing Numpy packageimport numpy as np  # declaring a listls =[1, 2, 3]  # converting the list into a Numpy arrayarr = 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 arraytry:    # 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")
Lists don't support list + int
Modified Numpy array: [5 6 7]

RISHU_MISHRA
python-list
Python-numpy
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python
sum() function in Python
How To Convert Python Dictionary To JSON?