Bird
Raised Fist0
NumPydata~20 mins

NumPy array vs Python list performance - Practice Questions

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following is true about NumPy arrays compared to Python lists when working with large numeric data?
easy
A. Python lists use less memory than NumPy arrays for numbers.
B. Python lists are faster for numeric calculations than NumPy arrays.
C. NumPy arrays and Python lists have the same speed for math operations.
D. NumPy arrays perform mathematical operations faster than Python lists.

Solution

  1. Step 1: Understand data structure differences

    NumPy arrays store data in a continuous block of memory optimized for numeric operations, unlike Python lists which store references to objects.
  2. Step 2: Compare performance for math operations

    This memory layout allows NumPy to perform vectorized operations much faster than Python lists, which require looping in Python code.
  3. Final Answer:

    NumPy arrays perform mathematical operations faster than Python lists. -> Option D
  4. Quick Check:

    NumPy arrays faster for math = D [OK]
Hint: NumPy arrays are optimized for math speed, unlike lists. [OK]
Common Mistakes:
  • Thinking Python lists are faster for math
  • Assuming both have same speed
  • Confusing memory usage with speed
2. Which of the following is the correct way to create a NumPy array from a Python list [1, 2, 3]?
easy
A. np.array([1, 2, 3])
B. np.list([1, 2, 3])
C. array([1, 2, 3])
D. np.array{1, 2, 3}

Solution

  1. Step 1: Recall NumPy array creation syntax

    The function to create a NumPy array is np.array() and it takes a list as input.
  2. Step 2: Check each option for syntax correctness

    np.array([1, 2, 3]) uses correct syntax. np.list([1, 2, 3]) uses a non-existent function np.list(). array([1, 2, 3]) misses the np. prefix. np.array{1, 2, 3} uses curly braces which is invalid.
  3. Final Answer:

    np.array([1, 2, 3]) -> Option A
  4. Quick Check:

    Correct NumPy array syntax = A [OK]
Hint: Use np.array() with square brackets for lists. [OK]
Common Mistakes:
  • Using curly braces instead of square brackets
  • Missing np prefix
  • Using non-existent np.list() function
3. What will be the output of the following code?
import numpy as np
import time

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

start = time.time()
sum_lst = sum(lst)
end = time.time()
print(round(end - start, 5))

start = time.time()
sum_arr = np.sum(arr)
end = time.time()
print(round(end - start, 5))
medium
A. The time to sum the list is less than the time to sum the NumPy array.
B. The time to sum the NumPy array is less than the time to sum the list.
C. Both times are exactly the same.
D. The code will raise an error because np.sum cannot sum arrays.

Solution

  1. Step 1: Understand sum operation on list vs NumPy array

    Python's built-in sum() loops over the list in Python code, which is slower. NumPy's np.sum() uses optimized C code for fast summation.
  2. Step 2: Predict timing results

    Because of this optimization, np.sum(arr) runs faster than sum(lst), so the printed time for NumPy sum is less.
  3. Final Answer:

    The time to sum the NumPy array is less than the time to sum the list. -> Option B
  4. Quick Check:

    NumPy sum faster than list sum = A [OK]
Hint: NumPy uses fast C code for sums, lists use slow Python loops. [OK]
Common Mistakes:
  • Assuming Python sum is faster
  • Thinking both times are equal
  • Believing np.sum cannot sum arrays
4. The following code tries to multiply each element of a Python list by 2 using NumPy syntax but fails. What is the error?
lst = [1, 2, 3, 4]
result = lst * 2
print(result)
medium
A. It raises a TypeError because lists cannot be multiplied.
B. It prints [2, 4, 6, 8] as expected.
C. It prints [1, 2, 3, 4, 1, 2, 3, 4] instead of doubling values.
D. It raises a NameError because 'lst' is undefined.

Solution

  1. Step 1: Understand list multiplication behavior

    Multiplying a Python list by 2 repeats the list elements, it does not multiply each element.
  2. Step 2: Compare with NumPy array behavior

    NumPy arrays multiply each element by 2, but Python lists just repeat the list when multiplied by an integer.
  3. Final Answer:

    It prints [1, 2, 3, 4, 1, 2, 3, 4] instead of doubling values. -> Option C
  4. Quick Check:

    List * 2 repeats list, does not multiply elements = C [OK]
Hint: List * 2 repeats list; use np.array for element-wise multiply. [OK]
Common Mistakes:
  • Expecting element-wise multiplication on lists
  • Thinking it raises an error
  • Confusing list and array multiplication
5. You have a large dataset of 1 million numbers stored as a Python list. You want to compute the mean efficiently. Which approach is best and why?
Option A:
mean = sum(lst) / len(lst)

Option B:
import numpy as np
arr = np.array(lst)
mean = np.mean(arr)

Option C:
mean = sum(lst) // len(lst)

Option D:
mean = np.sum(lst) / len(lst)
hard
A. Convert list to NumPy array and use np.mean for faster computation.
B. Use Python sum and len for mean; it's simple and fast enough.
C. Use integer division with sum and len for mean to speed up.
D. Use np.sum on list directly without conversion for best speed.

Solution

  1. Step 1: Evaluate each option's efficiency

    Use Python sum and len for mean; it's simple and fast enough. uses Python sum which is slower on large lists. Use integer division with sum and len for mean to speed up. uses integer division which loses precision and is not faster. Use np.sum on list directly without conversion for best speed. tries np.sum on a list which works but is slower than on arrays.
  2. Step 2: Identify best performance approach

    Convert list to NumPy array and use np.mean for faster computation. converts the list to a NumPy array and uses np.mean, which is optimized in C and much faster for large numeric data.
  3. Final Answer:

    Convert list to NumPy array and use np.mean for faster computation. -> Option A
  4. Quick Check:

    NumPy array + np.mean fastest for large numeric data = B [OK]
Hint: Convert list to NumPy array for fast numeric operations. [OK]
Common Mistakes:
  • Using integer division losing decimal precision
  • Using np.sum on list without conversion
  • Assuming Python sum is fast enough for large data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard