Recently, I was working on a data analysis project where I needed to initialize several empty arrays that would later be filled with calculation results. Having the right empty array structure from the start saves a lot of time and prevents errors when you’re working with large datasets.
In this article, I’ll cover multiple ways to create empty arrays in NumPy – from basic zero arrays to specialized empty functions.
Let’s get in!
Use Empty Arrays in NumPy
Empty arrays are incredibly useful when you know you’ll be filling an array with values later but want to pre-allocate memory for performance reasons. They’re also helpful for creating placeholder structures in complex algorithms.
Read NumPy: Create a NaN Array in Python
Create an Empty Array Using NumPy in Python
Now, I am going to explain to you how to create an empty array using NumPy in Python.
Method 1: Use np.empty()
NumPy’s empty() function in Python is the fastest way to create an empty array as it allocates memory without initializing the values.
import numpy as np
# Create a 1D empty array of size 5
empty_array_1d = np.empty(5)
print("1D Empty Array:")
print(empty_array_1d)
# Create a 2D empty array of shape (3, 4)
empty_array_2d = np.empty((3, 4))
print("\n2D Empty Array:")
print(empty_array_2d)Output:
1D Empty Array:
[3.33772792e-307 4.22786174e-307 2.78145267e-307 4.00537061e-307
1.29443977e-312]
2D Empty Array:
[[3.70908653e+006 7.89614587e+150 1.53549285e+223 3.09026652e+223]
[2.85747287e+161 7.86205671e-067 7.32845376e+025 1.71130458e+059]
[1.62980191e+045 2.67108442e+098 1.42681594e-312 1.39067116e-308]]I executed the above example code and added the screenshot below.

When you run this code, you’ll notice the array contains arbitrary values. This is because np.empty() it only allocates memory but doesn’t initialize it, making it very fast but potentially confusing if you forget to fill in all values later.
Check out NumPy Shape in Python
Method 2: Use np.zeros()
np.zeros() is used to create arrays filled with zeros, making it great for initializing known default values.
import numpy as np
# Create a 1D array of zeros with size 5
zeros_array_1d = np.zeros(5)
print("1D Zeros Array:")
print(zeros_array_1d)
# Create a 2D array of zeros with shape (3, 4)
zeros_array_2d = np.zeros((3, 4))
print("\n2D Zeros Array:")
print(zeros_array_2d)Output:
1D Zeros Array:
[0. 0. 0. 0. 0.]
2D Zeros Array:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]I executed the above example code and added the screenshot below.

This method ensures predictable results, especially useful when the initial array values must be zero.
Read 0-Dimensional Array NumPy in Python
Method 3: Use np.ones()
Use np.ones() to create arrays filled with the value 1, ideal for default-initialized datasets or masks.
import numpy as np
# Create a 1D array of ones with size 5
ones_array_1d = np.ones(5)
print("1D Ones Array:")
print(ones_array_1d)
# Create a 2D array of ones with shape (3, 4)
ones_array_2d = np.ones((3, 4))
print("\n2D Ones Array:")
print(ones_array_2d)Output:
1D Ones Array:
[1. 1. 1. 1. 1.]
2D Ones Array:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]I executed the above example code and added the screenshot below.

This method is reliable for generating uniform arrays with all ones across any shape.
Check out Python NumPy Matrix Operations
Method 4: Use np.full()
np.full() allows you to create arrays pre-filled with any specific value of your choice.
import numpy as np
# Create a 1D array filled with 7
full_array_1d = np.full(5, 7)
print("1D Array filled with 7:")
print(full_array_1d)
# Create a 2D array filled with 3.14
full_array_2d = np.full((3, 4), 3.14)
print("\n2D Array filled with 3.14:")
print(full_array_2d)It’s perfect when you need consistent values across an entire array for calculations or masking.
Method 5: Specify Data Types
Specifying the dtype in array creation functions optimizes memory usage and ensures compatibility with your computations.
import numpy as np
# Empty int array
empty_int = np.empty(5, dtype=int)
print("Empty int array:")
print(empty_int)
# Zeros float32 array
zeros_float32 = np.zeros(5, dtype=np.float32)
print("\nZeros float32 array:")
print(zeros_float32)
# Ones boolean array
ones_bool = np.ones(5, dtype=bool)
print("\nOnes boolean array:")
print(ones_bool)Setting the data type helps maintain precision and performance across different platforms.
Read Python NumPy Not Found: Fix Import Error
Method 6: Create Empty Arrays Like Existing Arrays
Functions like np.empty_like(), np.zeros_like(), and np.ones_like() clone shape and dtype from existing arrays.
import numpy as np
# Original array
original = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
# Empty array with same shape and type
empty_like = np.empty_like(original)
print("Empty array like original:")
print(empty_like)
# Zeros array with same shape and type
zeros_like = np.zeros_like(original)
print("\nZeros array like original:")
print(zeros_like)
# Ones array with same shape and type
ones_like = np.ones_like(original)
print("\nOnes array like original:")
print(ones_like)These are handy when building output arrays that mirror input arrays for operations or transformations.
Method 7: Create Structured Arrays
Structured arrays in NumPy allow you to handle complex data types, such as customer records, within a single array.
import numpy as np
# Define a structured data type for US customer data
dt = np.dtype([('name', 'U30'), ('state', 'U2'), ('age', np.int32), ('purchase_amount', np.float64)])
# Create an empty structured array with 5 elements
customer_data = np.empty(5, dtype=dt)
print("Empty structured array (US customer database):")
print(customer_data)
# Fill in some sample data
customer_data[0] = ('John Smith', 'CA', 34, 125.50)
customer_data[1] = ('Maria Garcia', 'TX', 28, 245.75)
print("\nPartially filled structured array:")
print(customer_data)This approach is powerful for representing tabular data, combining multiple fields of different types in one structure.
Check out Create a Matrix in Python
Performance Considerations
When working with large arrays, the initialization method matters. Let’s compare performance:
import numpy as np
import time
size = (5000, 5000)
# Time np.empty()
start = time.time()
empty_array = np.empty(size)
empty_time = time.time() - start
# Time np.zeros()
start = time.time()
zeros_array = np.zeros(size)
zeros_time = time.time() - start
print(f"Time to create empty array: {empty_time:.6f} seconds")
print(f"Time to create zeros array: {zeros_time:.6f} seconds")
print(f"np.empty() is {zeros_time/empty_time:.2f}x faster than np.zeros()")When I ran this on my machine, np.empty() was about 6x faster than np.zeros() for large arrays, which can make a significant difference in data-intensive applications.
Real-World Example: Image Processing Buffer
Here’s a practical example where pre-allocating empty arrays is useful in image processing:
import numpy as np
from PIL import Image
import time
# Load a sample image (simulate with random data for this example)
image = np.random.randint(0, 256, (1000, 1000, 3), dtype=np.uint8)
# Pre-allocate result arrays for various filters
gaussian_blur = np.empty_like(image)
edge_detect = np.empty_like(image)
sharpen = np.empty_like(image)
# In a real application, we would now apply the actual filters
# For demonstration, we'll just fill with dummy values
gaussian_blur[:] = image // 2
edge_detect[:] = image - gaussian_blur
sharpen[:] = image + edge_detectUsing pre-allocated arrays like this is much more efficient than creating new arrays for each operation, especially when processing multiple frames of video or batches of images.
I hope you found this article helpful for understanding how to create empty arrays in NumPy. Whether you need the speed of np.empty() or the safety of initialized arrays like np.zeros(), NumPy offers flexible options to fit your specific needs.
Remember that the right empty array initialization can significantly impact both the performance and memory usage of your Python applications, especially when working with large datasets or in performance-critical applications.
Other Python articles you may also like:
- np.genfromtxt() Function in Python
- NumPy Reset Index of an Array in Python
- Create a Python Empty Matrix

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.