Bird
0
0

You want to create a large array quickly and fill it later. Which is the best way to do this using np.empty()?

hard📝 Application Q15 of 15
NumPy - Creating Arrays
You want to create a large array quickly and fill it later. Which is the best way to do this using np.empty()?
import numpy as np
arr = np.empty((1000, 1000))
# Next step: fill arr with values

Why is np.empty() preferred here over np.zeros()?
ABecause <code>np.empty()</code> creates a copy of an existing array
BBecause <code>np.empty()</code> fills with zeros automatically
CBecause <code>np.empty()</code> is faster as it skips initialization
DBecause <code>np.empty()</code> initializes with random values
Step-by-Step Solution
Solution:
  1. Step 1: Understand performance difference

    np.empty() allocates memory without setting values, so it is faster than np.zeros() which sets all values to zero.
  2. Step 2: Reason why this is useful

    When you plan to fill the array later, skipping initialization saves time and resources.
  3. Final Answer:

    Because np.empty() is faster as it skips initialization -> Option C
  4. Quick Check:

    np.empty() = fast allocation without init [OK]
Quick Trick: Use np.empty() for speed when filling later [OK]
Common Mistakes:
  • Thinking np.empty() initializes with zeros
  • Confusing np.empty() with np.copy()
  • Assuming np.empty() fills with random values intentionally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes