Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
NumPy - Creating Arrays
What will be the output of this code?
import numpy as np
lst = [[1, 2], [3, 4]]
arr = np.array(lst)
print(arr.shape)
A(1, 4)
B(4,)
C(2, 2)
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the input list shape

    The list has 2 sublists, each with 2 elements, so shape is 2 rows and 2 columns.
  2. Step 2: Check numpy array shape property

    np.array converts nested lists into 2D arrays with shape (2, 2).
  3. Final Answer:

    (2, 2) -> Option C
  4. Quick Check:

    Nested list shape = (2, 2) [OK]
Quick Trick: Shape matches nested list dimensions [OK]
Common Mistakes:
  • Thinking shape is (4,)
  • Assuming shape is (1, 4)
  • Expecting an error for nested lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes