Signup/Sign In

How NumPy Arrays are better than Python List - Comparison with examples

Posted in Programming   LAST UPDATED: SEPTEMBER 7, 2021

    In the last tutorial, we got introduced to NumPy package in Python which is used for working on Scientific computing problems and that NumPy is the best when it comes to delivering the best high-performance multidimensional array objects and tools to work on them.

    Now, the question arises what is so special about NumPy arrays? Why can't we simply use python List for these scientific computations?

    Advantages of using NumPy Arrays:

    The most important benefits of using it are :

    • It consumes less memory.
    • It is fast as compared to the python List.
    • It is convenient to use.

    Now, let's write small programs to prove that NumPy multidimensional array object is better than the python List.

    Code 1: Comparing Memory use

    import numpy as np
    import time 
    import sys
    
    # Creating a NumPy array with 100 elements
    array = np.arange(100)
    # array.itemsize : Size of one element
    # array.size : length of array
    print("Size of NumPy array: ", array.itemsize * array.itemsize)
    
    # Creating a list with 100 elements
    # Now I'll print the size of list
    list = range(0, 10)
    # Multiplying size of 1 element with length of the list
    print("Size of list: ", sys.getsizeof(1)*len(list))

    Output:

    Size of NumPy array: 64
    Size of list: 280

    This clearly indicates that NumPy array consumes less memory as compared to the Python list.


    Code 2: Fast Computation of NumPy array

    import numpy as np
    import time
    import sys
    
    # let's declare the size
    Size = 100000
    
    # Creating two lists
    list1 = range(Size)
    list2 = range(Size)
    
    # Creating two NumPy arrays
    arr1 = np.arange(Size)
    arr2 = np.arange(Size)
    
    # Calculating time for Python list
    start = time.time()
    result = [(x y) for x, y in zip(list1, list2)]
              
    print("Time for Python List in msec: ", (time.time() - start) * 1000)
    
    # Calculating time for NumPy array
    start = time.time()
    result = arr1   arr2
    print("Time for NumPy array in msec: ", (time.time()- start) * 1000)
    
    print("\nThis means NumPy array is faster than Python List”)

    Output:

    Time for Python List in msec: 11.117696762084961
    Time for NumPy array in msec: 1.2216567993164062

    This means NumPy array is faster than Python List


    Code 3: NumPy is Convenient to use

    import numpy as np
    import time
    
    a1 = np.array([1, 2, 3])
    a2 = np.array([4, 5, 6])
    
    # To add two array you can simply do it by
    print("ADD a1 and a2 elements : ", a1   a2)
    
    # To sub two array you can simply do it by
    print("SUB a1 and a2 elements : ", a1 - a2)
    
    # To mul two array you can simply do it by
    print("MUL a1 and a2 elements : ", a1 * a2)
    
    # Calculating time for NumPy array
    start = time.time()
    result = arr1   arr2
    print("Time for NumPy array in msec: ", (time.time()- start) * 1000)

    Output:

    ADD a1 and a2 elements: [5 7 9]
    SUB a1 and a2 elements: [-3 -3 -3]
    MUL a1 and a2 elements: [ 4 10 18]


    All the above 3 code examples clearly validate the point that NumPy array is better than the Python list, when it comes to using for scientific computing problems which must use less memory, should be easy to use and are fast.

    So now we know what is NumPy, how to set it up, what are it's features and how it is way better than the python List. From the next tutorial, we will start with learning how to use this package.

    You may also like:

    About the author:
    Aspire to Inspire Before I Expire :)
    Tags:arraypythonnumpy
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS