Back to Python Notes
Topic #249

Joining and Splitting Arrays

Learn how to combine multiple NumPy arrays into one and split a single array into smaller parts using np.concatenate, np.split, and related functions.

What it is

In numerical computing, you often need to merge separate data sources or break large datasets into manageable chunks. NumPy provides efficient tools for these operations. Joining (concatenation) combines arrays along an existing axis, while splitting divides an array into sub-arrays. These operations are fundamental in data preprocessing, image processing, and batched computations.

Key terms include:

  • Axis: The dimension along which the operation occurs (0 for rows, 1 for columns).
  • Concatenate: Joining arrays end-to-end.
  • Split: Dividing an array at specific indices.

Why it matters

  • Data Aggregation: Combine results from parallel processes or different sensors into a single dataset.
  • Batch Processing: Split large matrices into smaller batches for machine learning training.
  • Image Manipulation: Stitch together image tiles or crop images by splitting pixel arrays.
  • Memory Efficiency: Avoid creating unnecessary copies when possible by using views where applicable.

Syntax or steps

The primary function for joining is np.concatenate((a, b), axis=0). For splitting, use np.split(a, indices_or_sections, axis=0).

  1. Joining: Pass a tuple of arrays to np.concatenate. Specify the axis if not joining along the first dimension.
  2. Splitting: Provide the array and either an integer (equal splits) or a list of indices (custom splits) to np.split.
  3. Alternative Joins: Use np.vstack for vertical stacking (rows) and np.hstack for horizontal stacking (columns) for simpler syntax.

Example

import numpy as np

# Create two sample arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

# Joining: Concatenate along rows (axis=0)
joined_rows = np.concatenate((a, b), axis=0)
print("Joined Rows:\n", joined_rows)

# Joining: Concatenate along columns (axis=1)
joined_cols = np.concatenate((a, b), axis=1)
print("\nJoined Cols:\n", joined_cols)

# Splitting: Split 'joined_rows' into two equal parts along axis 0
parts = np.split(joined_rows, 2, axis=0)
print("\nSplit Part 1:\n", parts[0])
print("Split Part 2:\n", parts[1])

Explanation:

  • a and b are 2x2 arrays.
  • np.concatenate((a, b), axis=0) stacks them vertically, resulting in a 4x2 array.
  • np.concatenate((a, b), axis=1) stacks them horizontally, resulting in a 2x4 array.
  • np.split(joined_rows, 2, axis=0) cuts the 4x2 array into two 2x2 arrays at the midpoint of the row axis.

Common mistakes

  • Mismatched Dimensions: Arrays must have identical shapes except for the concatenation axis. Fix by reshaping or padding arrays before joining.
  • Incorrect Axis: Using axis=1 on 1D arrays raises an error. Ensure your arrays are at least 2D if splitting/joining along columns, or use np.column_stack.
  • Uneven Splits: np.split requires the number of sections to divide the length evenly. If not, use np.array_split which allows uneven sizes.
  • Confusing Views vs Copies: While concatenate creates a new array, some slicing operations return views. Be cautious when modifying split parts if they share memory with the original.

When to use it

OperationUse CaseAlternative
np.concatenate General purpose joining along any axis. np.vstack/np.hstack for simple 2D cases.
np.split Dividing into equal-sized chunks. np.array_split for unequal chunks.
np.stack Creating a new axis (e.g., combining 2D images into a 3D batch). np.expand_dims + concatenate.

Practice

Guided Exercise: Create three 1D arrays of length 3. Stack them vertically to form a 3x3 matrix, then split this matrix into three 1D arrays again.

Challenge: Given a 4x4 identity matrix, split it into four 2x2 blocks using np.split twice (once for rows, once for columns). Hint: You may need to iterate over the results.

Quick check

Q: What happens if you try to concatenate two arrays with shape (2,3) and (2,4) along axis=1?

A: It works successfully, resulting in an array of shape (2,7), because the non-concatenated dimensions (axis 0) match.

Summary

NumPy’s join and split functions allow flexible manipulation of array structures without manual loops. Mastering axis parameters and understanding shape compatibility ensures efficient data handling in scientific Python workflows.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Joining and Splitting Arrays – FAQs

Quick answers about learning Joining and Splitting Arrays in Python.

This free note from Coding Now Tech Institute explains Joining and Splitting Arrays in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Joining and Splitting Arrays, is 100% free with no signup required.
With focused practice, most students grasp Joining and Splitting Arrays in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now