What is the difference between a NumPy array and a list?

Practicing Coding Interview Questions in Python

Kirill Smirnov

Data Science Consultant, Altran

NumPy array

import numpy as np
num_array = np.array([1, 2, 3, 4, 5])
print(num_array)
[1 2 3 4 5]
num_list = [1, 2, 3, 4, 5]
print(num_list)
[1, 2, 3, 4, 5]
Practicing Coding Interview Questions in Python

Similarities between an array and a list

num_array = np.array([1, 2, 3, 4, 5])
for item in num_array:
    print(item)
1
2
3
4
5
num_list = [1, 2, 3, 4, 5]
for item in num_list:
    print(item)
1
2
3
4
5
Practicing Coding Interview Questions in Python

Similarities between an array and a list

num_array = np.array([1, 2, 3, 4, 5])
num_array[1]
2
num_array[1:4]
array([2, 3, 4])
num_list = [1, 2, 3, 4, 5]
num_list[1]
2
num_list[1:4]
[2, 3, 4]
Practicing Coding Interview Questions in Python

Similarities between an array and a list

num_array = np.array([1, 2, 3, 4, 5])
num_array[3] = 40
print(num_array)
[1 2 3 40 5]
num_array[0:3] = [10, 20, 30]
print(num_array)
[10 20 30 40 5]
num_list = [1, 2, 3, 4, 5]
num_list[3] = 40
print(num_list)
[1, 2, 3, 40, 5]
num_list[0:3] = [10, 20, 30]
print(num_list)
[10, 20, 30, 40, 5]
Practicing Coding Interview Questions in Python

Difference between an array an a list

NumPy arrays are designed for high efficiency computations

  • NumPy arrays store values of the same type
Practicing Coding Interview Questions in Python

.dtype property

num_array = np.array([1, 2, 3, 4, 5])
num_array.dtype
dtype('int64')
Practicing Coding Interview Questions in Python

Changing the data type of an element

num_array = np.array([1, 2, 3, 4, 5])
num_array[2] = 'three'
ValueError
num_list = [1, 2, 3, 4, 5]
num_list[2] = 'three'
print(num_list)
[1, 2, 'three', 4, 5]
Practicing Coding Interview Questions in Python

Specifying the data type explicitly

num_array = np.array([1, 2, 3, 4, 5])
num_array = np.array([1, 2, 3, 4, 5], dtype = np.dtype('int64'))
print(num_array)
[1 2 3 4 5]
num_array.dtype
dtype('int64')
Practicing Coding Interview Questions in Python

Specifying the data type explicitly

num_array = np.array([1, 2, 3, 4, 5])
num_array = np.array([1, 2, 3, 4, 5], dtype = np.dtype('str'))
print(num_array)
['1' '2' '3' '4' '5']
num_array.dtype
dtype('<U1')
Practicing Coding Interview Questions in Python

Object as a data type

num_array = np.array([1, 2, 3, 4, 5], dtype = np.dtype('O'))
num_array[2] = 'three'
print(num_array)
[1 2 'three' 4 5]
Practicing Coding Interview Questions in Python

Difference between an array and a list

NumPy arrays are designed for high efficiency computations

  • NumPy arrays store values of a concrete data type
  • NumPy arrays have a special way to access its elements
Practicing Coding Interview Questions in Python

Accessing items

list2d = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]
# Retrieve 8
list2d[1][2]
8
array2d = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
])
# Retrieve 8
array2d[1][2]
8
Practicing Coding Interview Questions in Python

Accessing items

list2d = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]
# Retrieve 8
list2d[1][2]
8
array2d = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
])
# Retrieve 8
array2d[1, 2]
8
Practicing Coding Interview Questions in Python

Accessing items

list2d = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]
# Retrieve [[2, 3, 4], [7, 8, 9]]
array2d = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
])
# Retrieve [[2, 3, 4], [7, 8, 9]]
Practicing Coding Interview Questions in Python

Accessing items

list2d = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]
# Retrieve [[2, 3, 4], [7, 8, 9]]
[
    [list2d[j][1:4] for j in range(0, 2)]
]
[[2, 3, 4], [7, 8, 9]]
array2d = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
])
# Retrieve [[2, 3, 4], [7, 8, 9]]
Practicing Coding Interview Questions in Python

Accessing items

list2d = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
]
# Retrieve [[2, 3, 4], [7, 8, 9]]
[
    [list2d[j][1:4] for j in range(0, 2)]
]
[[2, 3, 4], [7, 8, 9]]
array2d = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15]
])
# Retrieve [[2, 3, 4], [7, 8, 9]]
array2d[0:2, 1:4]
array([[2, 3, 4],
       [7, 8, 9]])
Practicing Coding Interview Questions in Python

Difference between an array and a list

NumPy arrays are designed for high efficiency computations

  • NumPy arrays store values of a concrete data type
  • NumPy arrays have a special way to access its elements
  • NumPy arrays have efficient way to perform operations on them.
Practicing Coding Interview Questions in Python

Operations +, -, *, / with lists

num_list1 = [1, 2, 3]
num_list2 = [10, 20, 30]
num_list1 + num_list2
[1, 2, 3, 10, 20, 30]
num_list2 - num_list1
TypeError
num_list1 * num_list2
TypError
num_list2 / num_list1
TypeError
Practicing Coding Interview Questions in Python

Operations +, -, *, / with arrays

num_array1 = np.array([1, 2, 3])
num_array2 = np.array([10, 20, 30])
num_array1 + num_array2
array([11, 22, 33])
num_array2 - num_array1
array([9, 18, 27])
num_array1 * num_array2
array([10, 40, 90])
num_array2 / num_array1
array([10, 10, 10])
Practicing Coding Interview Questions in Python

Operations +, -, *, / with multidimensional arrays

num_array1 = np.array([
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
    [11, 12, 13,14, 15]
])
num_array2 = np.array([
    [10, 20, 30, 40, 50],
    [60, 70, 80, 90, 100],
    [110, 120, 130,140, 150]
])
num_array1 + num_array2
array([[ 11,  22,  33,  44,  55],
       [ 66,  77,  88,  99, 110],
       [121, 132, 143, 154, 165]])
num_array2 / num_array1
array([[10., 10., 10., 10., 10.],
       [10., 10., 10., 10., 10.],
       [10., 10., 10., 10., 10.]])
Practicing Coding Interview Questions in Python

Conditional operations

>, <, >=, <=, ==, !=

num_array = np.array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])
num_array < 0
array([True,  True,  True, False, False, False, False])
num_array[num_array < 0]
array([-5, -4, -3, -2, -1])
Practicing Coding Interview Questions in Python

Broadcasting

num_array = np.array([1, 2, 3])
num_array * 3
array([3, 6, 9])
num_array + 3
array([4, 5, 6])
num_list = [1, 2, 3]
num_list * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Practicing Coding Interview Questions in Python

Broadcasting with multidimensional arrays

array2d $\left(3 \text{ x } 4\right)$

array2d = np.array([
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4]
])

array1d $\left(1 \text{ x } 4\right)$

array1d = np.array([1, 2, 3, 4])
array2d / array1d
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
Practicing Coding Interview Questions in Python

Broadcasting with multidimensional arrays

array2d $\left(3 \text{ x } 4\right)$

array2d = np.array([
    [1, 2, 3, 4],
    [1, 2, 3, 4],
    [1, 2, 3, 4]
])

array1d $\left(3 \text{ x } 1\right)$

array1d = np.array([[1], [2], [3]])
array2d / array1d
array([[1.   , 2.   , 3.   , 4.   ],
       [0.5  , 1.   , 1.5  , 2.   ],
       [0.333, 0.667, 1.   , 1.333]])
Practicing Coding Interview Questions in Python

Let's practice

Practicing Coding Interview Questions in Python

Preparing Video For Download...