🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.empty()

AI & DATA SCIENCE // np-empty

np.empty() creates a new array of a given shape without initializing its values, leaving whatever arbitrary data happens to already be in that memory.

Syntax

np.empty(shape, dtype=float)

Deep Dive Course

Unlike np.zeros() or np.ones(), np.empty() skips the step of writing initial values into the allocated memory entirely, which makes it marginally faster to create, but means its contents are unpredictable garbage — whatever bytes happened to be in that memory already, possibly leftover data from a previous, unrelated array. It only makes sense to use when you're certain every element will be overwritten before it's ever read, such as immediately before a loop that fills in every index.

1Understanding np.empty()

Unlike np.zeros() or np.ones(), np.empty() skips the step of writing initial values into the allocated memory entirely, which makes it marginally faster to create, but means its contents are unpredictable garbage — whatever bytes happened to be in that memory already, possibly leftover data from a previous, unrelated array. It only makes sense to use when you're certain every element will be overwritten before it's ever read, such as immediately before a loop that fills in every index.

💡

Never read from an np.empty() array before writing to every element — its initial contents are genuinely undefined garbage, not zeros, and can even change between runs of the same program.

editor.html
import numpy as np

arr = np.empty(3)
print(arr.shape)
print(arr.dtype)
localhost:3000

2Practical Example

Here is a real-world application of np.empty() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.empty(5)
for i in range(5):
    arr[i] = i ** 2
print(arr)
localhost:3000

3Best Practices

Follow these guidelines when working with np.empty():

1. Use np.empty() only when you will overwrite every single element before reading any of them, such as right before a fill loop

2. Prefer np.zeros() by default unless you've specifically measured that np.empty()'s tiny initialization-skipping speedup matters for your use case

3. Never rely on np.empty()'s initial values for any logic — treat them as completely undefined

⚠️

Tip: Never read from an np.empty() array before writing to every element — its initial contents are genuinely undefined garbage, not zeros, and can even change between runs of the same program.

editor.html
import numpy as np

arr = np.empty(3)
print(arr.shape)
print(arr.dtype)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.empty(3)
print(arr.shape)
print(arr.dtype)
Example 02Advanced Example
import numpy as np

arr = np.empty(5)
for i in range(5):
    arr[i] = i ** 2
print(arr)

Best Practices

  • Use np.empty() only when you will overwrite every single element before reading any of them, such as right before a fill loop
  • Prefer np.zeros() by default unless you've specifically measured that np.empty()'s tiny initialization-skipping speedup matters for your use case
  • Never rely on np.empty()'s initial values for any logic — treat them as completely undefined

Interview Question

Why shouldn't you ever read a value from an np.empty() array before explicitly writing to that position?

Hint: Think about what np.empty() actually does, or rather doesn't do, during allocation.

np.empty() only reserves a block of memory of the requested size without writing any initial values into it — whatever data was already sitting in that memory from a previous allocation remains there, effectively random from the caller's perspective. Reading a position before writing to it returns that leftover garbage, which is not a stable, reproducible value like 0, so any logic depending on it would behave unpredictably and potentially differently across runs or even machines.

Exercises

MediumPractice using np.empty() in a real scenario.
View Solution
import numpy as np

arr = np.empty(3)
print(arr.shape)
print(arr.dtype)

Frequently Asked Questions

Why shouldn't you ever read a value from an np.empty() array before explicitly writing to that position?

np.empty() only reserves a block of memory of the requested size without writing any initial values into it — whatever data was already sitting in that memory from a previous allocation remains there, effectively random from the caller's perspective. Reading a position before writing to it returns that leftover garbage, which is not a stable, reproducible value like 0, so any logic depending on it would behave unpredictably and potentially differently across runs or even machines.

Related Functions

np-zerosnp-onesnp-full