0
0
NumPydata~20 mins

NumPy array vs Python list performance - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy vs Python List Performance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Comparing sum performance of NumPy array and Python list
What is the output of the following code snippet that compares the sum of elements in a NumPy array and a Python list?
NumPy
import numpy as np
import time

arr = np.arange(1000000)
lst = list(range(1000000))

start_np = time.time()
sum_np = np.sum(arr)
end_np = time.time()

start_lst = time.time()
sum_lst = sum(lst)
end_lst = time.time()

print(f"NumPy sum: {sum_np}, Time: {end_np - start_np:.6f} seconds")
print(f"List sum: {sum_lst}, Time: {end_lst - start_lst:.6f} seconds")
A
NumPy sum: 499999500000, Time: less than 0.01 seconds
List sum: 499999500000, Time: more than 0.01 seconds
B
NumPy sum: 499999500000, Time: more than 0.1 seconds
List sum: 499999500000, Time: less than 0.01 seconds
C
NumPy sum: 499999500000, Time: more than 1 second
List sum: 499999500000, Time: more than 1 second
D
NumPy sum: 0, Time: less than 0.01 seconds
List sum: 0, Time: less than 0.01 seconds
Attempts:
2 left
💡 Hint
Think about how NumPy uses optimized C code for operations compared to Python's built-in sum.
data_output
intermediate
2:00remaining
Memory usage difference between NumPy array and Python list
What is the approximate memory usage difference when storing one million integers in a NumPy array vs a Python list?
NumPy
import numpy as np
import sys

arr = np.arange(1000000, dtype=np.int32)
lst = list(range(1000000))

mem_arr = arr.nbytes
mem_lst = sys.getsizeof(lst) + sum(sys.getsizeof(i) for i in lst)

print(f"NumPy array memory: {mem_arr} bytes")
print(f"Python list memory: {mem_lst} bytes")
A
NumPy array memory: about 10,000,000 bytes
Python list memory: about 10,000,000 bytes
B
NumPy array memory: about 28,000,000 bytes
Python list memory: about 4,000,000 bytes
C
NumPy array memory: about 4,000,000 bytes
Python list memory: about 28,000,000 bytes
D
NumPy array memory: about 1,000,000 bytes
Python list memory: about 1,000,000 bytes
Attempts:
2 left
💡 Hint
Consider that Python lists store references to objects, while NumPy arrays store raw data in contiguous memory.
visualization
advanced
2:00remaining
Visualizing performance difference of element-wise multiplication
Which option produces the correct matplotlib bar chart comparing the time taken for element-wise multiplication of a NumPy array vs a Python list?
NumPy
import numpy as np
import time
import matplotlib.pyplot as plt

arr = np.arange(1000000)
lst = list(range(1000000))

start_np = time.time()
res_np = arr * 2
end_np = time.time()

start_lst = time.time()
res_lst = [x * 2 for x in lst]
end_lst = time.time()

times = [end_np - start_np, end_lst - start_lst]
labels = ['NumPy array', 'Python list']

plt.bar(labels, times, color=['blue', 'orange'])
plt.ylabel('Time (seconds)')
plt.title('Element-wise multiplication performance')
plt.show()
ANo chart displayed due to error in plotting code
BBar chart with Python list bar much shorter than NumPy bar, showing Python list is faster
CBar chart with both bars equal height, showing no difference
DBar chart with NumPy bar much shorter than Python list bar, showing NumPy is faster
Attempts:
2 left
💡 Hint
NumPy uses vectorized operations that are faster than list comprehensions.
🧠 Conceptual
advanced
2:00remaining
Why are NumPy arrays faster than Python lists for numerical operations?
Which option best explains why NumPy arrays perform numerical operations faster than Python lists?
ANumPy arrays convert data to strings before operations to speed up processing
BNumPy arrays store data in contiguous memory blocks and use optimized C code for operations
CPython lists use multi-threading internally which slows down operations
DPython lists are immutable, so operations require copying data
Attempts:
2 left
💡 Hint
Think about how data is stored and processed at a low level.
🔧 Debug
expert
2:00remaining
Identifying error in mixing NumPy arrays and Python lists in arithmetic
What error will this code raise? import numpy as np arr = np.array([1, 2, 3]) lst = [4, 5, 6] result = arr + lst print(result)
A[5 7 9]
BValueError: operands could not be broadcast together with shapes (3,) (3,)
CTypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'list'
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
NumPy supports adding arrays and lists element-wise by converting lists to arrays.