How do I create an empty array and then append to it in NumPy?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
While you can create an empty NumPy array with np.empty() or np.array([]) and append to it with np.append(), this pattern is almost always wrong because NumPy arrays have fixed sizes — every np.append() creates a new array and copies all data. The recommended approach is to build a Python list first, then convert to a NumPy array at the end. Alternatively, pre-allocate an array with np.zeros() or np.empty() if you know the final size.
The Append Anti-Pattern (Avoid This)
Each np.append() allocates a new array of size n+1 and copies all existing elements. For 10,000 elements, this copies a total of ~50 million elements.
Recommended: Build a List, Then Convert
This is the standard pattern. Python lists are designed for dynamic appending, while NumPy arrays are designed for fixed-size numerical computation.
With 2D Data
Recommended: Pre-Allocate with Known Size
If you know the final size, pre-allocate and fill by index:
np.empty() is slightly faster than np.zeros() because it does not initialize the memory, but the values are garbage until you write to them.
Creating Empty Arrays
np.empty — Uninitialized
np.zeros — Filled with Zeros
np.array([]) — Truly Empty
Using np.append (When Necessary)
np.append() returns a new array — it does not modify the original:
Appending Rows to a 2D Array
np.concatenate and np.vstack
For combining existing arrays, np.concatenate is more explicit than np.append:
Performance Comparison
Common Pitfalls
- Using
np.append()in a loop: Every call copies the entire array, resulting in O(n^2) time complexity. Build a Python list and convert withnp.array()at the end, or pre-allocate withnp.zeros()/np.empty(). - Assuming
np.append()modifies in place: Unlikelist.append(),np.append()returns a new array. The original is unchanged. You must assign the result:arr = np.append(arr, value). - Forgetting
axisparameter in 2D append: Withoutaxis,np.append()flattens both arrays into 1D. Useaxis=0to append rows oraxis=1to append columns. - Using
np.empty()without initializing all elements:np.empty()contains garbage values from memory. If you skip some indices, those positions contain unpredictable data. Usenp.zeros()ornp.full()if you need a default value. - Creating arrays with mismatched dtypes: Appending a float to an int array converts the entire array to float. Specify
dtypeat creation time to avoid unexpected type promotions.
Summary
- Avoid
np.append()in loops — it copies the entire array each time, making it O(n^2) - Build a Python list with
list.append(), then convert to NumPy withnp.array(lst)at the end - Pre-allocate with
np.zeros(n)ornp.empty(n)and fill by index when the size is known - Use
np.concatenate()ornp.vstack()to combine multiple existing arrays in one operation np.append()returns a new array — it does not modify the original

