← Back to NumPy Course | Chapter 5: Array Manipulation | Lesson 4 of 7

concatenate()

concatenate() joins arrays end to end along an axis you choose.

In this page:

  1. concatenate()
Syntax
python
np.concatenate((arr1, arr2), axis=0)

concatenate()

np.concatenate takes a tuple of arrays and joins them along an existing axis. The arrays must match in every other dimension. axis=0 stacks rows, axis=1 adds columns.

Note: axis=None flattens everything before joining.

Example: concatenate()

python
import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
print(np.concatenate((a, b), axis=0))
print(np.concatenate((a, a), axis=1))

# Output:
# [[1 2]
#  [3 4]
#  [5 6]]
# [[1 2 1 2]
#  [3 4 3 4]]
Related Topics
Common Mistakes
  1. Passing arrays as separate arguments instead of a tuple
  2. Joining arrays with mismatched shapes
  3. Confusing axis 0 and axis 1
Chapter Summary
  • Pass a tuple of arrays
  • Other dimensions must match
  • axis picks the join direction
  • Returns a new array
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.