🚀 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.append()

AI & DATA SCIENCE // np-append

np.append() returns a new array with additional values added to the end of an existing array, always allocating a brand-new array rather than growing the original in place.

Syntax

np.append(arr, values, axis=None)

Deep Dive Course

Unlike a Python list's append(), which grows in place, np.append() always creates and returns an entirely new array combining the original data with the appended values, since NumPy arrays have a fixed size and cannot actually be extended in place. Without an axis argument, both arrays are first flattened before being joined into a 1D result; with an axis specified, values must have a compatible shape along the other dimensions, behaving much like np.concatenate() with an extra convenience for appending simple values.

1Understanding np.append()

Unlike a Python list's append(), which grows in place, np.append() always creates and returns an entirely new array combining the original data with the appended values, since NumPy arrays have a fixed size and cannot actually be extended in place. Without an axis argument, both arrays are first flattened before being joined into a 1D result; with an axis specified, values must have a compatible shape along the other dimensions, behaving much like np.concatenate() with an extra convenience for appending simple values.

💡

Calling np.append() repeatedly inside a loop is a common but seriously inefficient anti-pattern — each call copies the entire array so far into a new, larger one, making the total cost quadratic; accumulate values in a plain Python list and convert to an array once at the end instead.

editor.html
import numpy as np

arr = np.array([1, 2, 3])
result = np.append(arr, [4, 5])
print(result)
localhost:3000

2Practical Example

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

editor.html
import numpy as np

matrix = np.array([[1, 2], [3, 4]])
result = np.append(matrix, [[5, 6]], axis=0)
print(result)
localhost:3000

3Best Practices

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

1. Never call np.append() repeatedly inside a loop — accumulate values in a Python list first, then convert to an array once with np.array() at the end

2. Use np.concatenate() directly when combining full arrays, reserving np.append() for the specific case of adding a handful of extra values

3. Pre-allocate an array with np.zeros()/np.empty() and fill it by index instead of building it up via repeated append() calls, whenever the final size is known ahead of time

⚠️

Tip: Calling np.append() repeatedly inside a loop is a common but seriously inefficient anti-pattern — each call copies the entire array so far into a new, larger one, making the total cost quadratic; accumulate values in a plain Python list and convert to an array once at the end instead.

editor.html
import numpy as np

arr = np.array([1, 2, 3])
result = np.append(arr, [4, 5])
print(result)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 3])
result = np.append(arr, [4, 5])
print(result)
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2], [3, 4]])
result = np.append(matrix, [[5, 6]], axis=0)
print(result)

Best Practices

  • Never call np.append() repeatedly inside a loop — accumulate values in a Python list first, then convert to an array once with np.array() at the end
  • Use np.concatenate() directly when combining full arrays, reserving np.append() for the specific case of adding a handful of extra values
  • Pre-allocate an array with np.zeros()/np.empty() and fill it by index instead of building it up via repeated append() calls, whenever the final size is known ahead of time

Interview Question

Why is calling np.append() inside a loop, to build up an array one piece at a time, considered a serious performance anti-pattern?

Hint: Think about what has to happen in memory on every single call.

Since NumPy arrays have a fixed size, every call to np.append() must allocate a brand-new array large enough for the combined result and copy the entire existing array's contents into it before adding the new values. Doing this repeatedly inside a loop means each iteration re-copies an array that keeps growing, resulting in roughly quadratic total time as the loop progresses — accumulating values in a Python list, which does support efficient in-place growth, and converting to a NumPy array once at the end avoids this entirely.

Exercises

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

arr = np.array([1, 2, 3])
result = np.append(arr, [4, 5])
print(result)

Frequently Asked Questions

Why is calling np.append() inside a loop, to build up an array one piece at a time, considered a serious performance anti-pattern?

Since NumPy arrays have a fixed size, every call to np.append() must allocate a brand-new array large enough for the combined result and copy the entire existing array's contents into it before adding the new values. Doing this repeatedly inside a loop means each iteration re-copies an array that keeps growing, resulting in roughly quadratic total time as the loop progresses — accumulating values in a Python list, which does support efficient in-place growth, and converting to a NumPy array once at the end avoids this entirely.

Related Functions

np-insertnp-deletenp-concatenate