0
0
NumPydata~20 mins

Why NumPy over Python lists - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NumPy Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Comparing speed of element-wise addition
What is the output of the following code showing the time taken for element-wise addition using Python lists vs NumPy arrays?
NumPy
import time
import numpy as np

size = 1000000
list1 = list(range(size))
list2 = list(range(size))

start = time.time()
result_list = [list1[i] + list2[i] for i in range(size)]
end = time.time()
list_time = end - start

arr1 = np.arange(size)
arr2 = np.arange(size)

start = time.time()
result_np = arr1 + arr2
end = time.time()
np_time = end - start

print(f"List time: {list_time:.4f} seconds")
print(f"NumPy time: {np_time:.4f} seconds")
ANumPy time is significantly higher than list time, showing lists are faster
BList time and NumPy time are about the same
CList time is significantly higher than NumPy time, showing NumPy is faster
DBoth times are zero seconds due to fast execution
Attempts:
2 left
💡 Hint
Think about how Python loops compare to NumPy's optimized operations.
data_output
intermediate
2:00remaining
Memory usage difference between list and NumPy array
What is the output of the following code comparing memory usage of a Python list and a NumPy array of the same size?
NumPy
import sys
import numpy as np

size = 1000000
list1 = list(range(size))
arr1 = np.arange(size)

list_mem = sys.getsizeof(list1) + sum(sys.getsizeof(x) for x in list1)
arr_mem = arr1.nbytes

print(f"List memory: {list_mem} bytes")
print(f"NumPy array memory: {arr_mem} bytes")
AList memory is much larger than NumPy array memory
BNumPy array memory is zero bytes
CBoth have the same memory usage
DList memory is smaller than NumPy array memory
Attempts:
2 left
💡 Hint
Consider how Python lists store references vs NumPy arrays store raw data.
🧠 Conceptual
advanced
2:00remaining
Why does NumPy use less memory than Python lists?
Which reason best explains why NumPy arrays use less memory than Python lists?
ANumPy arrays store data as strings, which are smaller than numbers.
BNumPy arrays store elements as raw typed data in contiguous memory blocks, while lists store pointers to objects.
CPython lists store data in a binary format, which uses more space.
DNumPy arrays compress data automatically, while lists do not.
Attempts:
2 left
💡 Hint
Think about how data is stored in memory for each type.
🔧 Debug
advanced
2:00remaining
Identifying error in mixed-type NumPy array creation
What error or behavior occurs when running this code?
NumPy
import numpy as np

arr = np.array([1, 2, '3', 4])
print(arr.dtype)
print(arr + 1)
AThe array has dtype '<U21' (string) and adding 1 raises a TypeError
BThe array has dtype int and adding 1 works fine
CThe array has dtype float and adding 1 works fine
DThe code raises a SyntaxError
Attempts:
2 left
💡 Hint
Check how NumPy handles mixed data types in arrays.
🚀 Application
expert
2:00remaining
Choosing NumPy for large-scale numerical computations
You need to perform matrix multiplication on two large datasets efficiently. Which reason best supports choosing NumPy arrays over Python lists?
APython lists can only store strings, so they cannot be used for matrix multiplication.
BPython lists automatically parallelize operations, making them faster for large data.
CNumPy arrays use more memory but are easier to read.
DNumPy arrays support optimized, vectorized operations implemented in C, making matrix multiplication faster and more memory efficient.
Attempts:
2 left
💡 Hint
Think about speed and memory when working with large numerical data.