Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q7 of 15
NumPy - Creating Arrays
Identify the error in this code snippet:
import numpy as np
lst = [[1, 2], [3, 4, 5]]
arr = np.array(lst)
print(arr.shape)
ANo error, shape is (2,)
BValueError due to uneven inner list lengths
CSyntaxError in np.array() call
DTypeError: cannot convert list to array
Step-by-Step Solution
Solution:
  1. Step 1: Check inner list lengths

    Inner lists have different lengths: 2 and 3.
  2. Step 2: Effect on numpy array shape

    Numpy creates an array of dtype=object with shape (2,), treating inner lists as objects.
  3. Final Answer:

    No error, shape is (2,) -> Option A
  4. Quick Check:

    Uneven nested lists create 1D object arrays [OK]
Quick Trick: Uneven nested lists create 1D object arrays [OK]
Common Mistakes:
  • Expecting 2D array shape
  • Assuming ValueError raised
  • Confusing syntax and type errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes