IKH

Array

  • An indexed collection of homogenous data elements is nothing but array.
  • It is the most commonly used concept in programming language like C/C++, java … etc.
  • By default arrays concept is not available in python, instead we can use List.
  • In python, we can create arrays in the following 2 ways:
    • Using array module.
    • Using numpy module.

ndarray in numpy

  • In numpy data is stored in the form of array.
  • The arrays which are created by using numpy are called ndarray.
  • ndarray ⇒ n-dimensional array or numpy array.

Python list vs numpy array

  • Python lists and numpy arrays are both container data types that can hold a collection of items. They are used in different contexts and have different characteristics, making each of them suitable for particular tasks.

Similarities

  • Both can be used to store data.
  • The order will be preserved in both. Hence indexing and slicing concepts are applicable.
  • Both are mutable, i.e. we can change the content

Differences

  • List is python’s inbuilt type. But we have to install and import numpy explicitly.
  • List can contain heterogeneous elements. But array contains only homogeneous elements.
  • On list, we cannot perform vector operations. But on ndarray we can perform vector operations.
  • Arrays consume less memory than list.
  • Arrays are superfast when compared with list.
  • Numpy arrays are more convenient to use while performing complex mathematical operations.

Example

  • Write a program to show in list we cannot perform vector operations. But on ndarray we can perform vector operations.
Python
import numpy as np
my_list = [10,20,30,40]
my_array = np.array([10,20,30,40])
#print(my_list+2)
print(my_array+2)

Output

PowerShell
#TypeError: can only concatenate list (not "int") to list
[12 22 32 42]

Example

  • Write a program to show arrays consume less memory than list.
Python
import numpy as np
import sys
my_list = [10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100]
my_array = np.array([10,20,30,40,50,60,70,80,90,100,10,20,30,40,50,60,70,80,90,100])
print('The Size of list => ',sys.getsizeof(my_list))
print('The Size of ndarray => ',sys.getsizeof(my_array))

Output

PowerShell
The Size of list => 216
The Size of ndarray => 184

Example

  • Write a program to show arrays are superfast when compared with list.
Python
import numpy as np
from datetime import datetime
array1 = np.array([10,20,30])
array2 = np.array([1,2,3])

#traditional python code
def dot_product(a,b):
 result = 0
 for i,j in zip(a,b):
   result = result + i*j
 return result

before = datetime.now()
for i in range(1000000):
 dot_product(array1,array2)
after = datetime.now()
print('The Time taken by traditonal python:',after-before)

#numpy library code
before = datetime.now()
for i in range(1000000):
 np.dot(array1,array2)
after = datetime.now()
print('The Time taken by Numpy Library:',after-before)

Output

PowerShell
The Time taken by traditonal python: 0:00:01.580073
The Time taken by Numpy Library: 0:00:01.031358

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error