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

AI & DATA SCIENCE // np-hstack

np.hstack() stacks a sequence of arrays horizontally, column by column for 2D arrays or end-to-end for 1D arrays, equivalent to concatenating along axis 1 (or axis 0 for 1D inputs).

Syntax

np.hstack((arr1, arr2, ...))

Deep Dive Course

For 2D arrays, hstack() places inputs side by side, requiring them to have the same number of rows, equivalent to np.concatenate(..., axis=1). For 1D arrays specifically, hstack() behaves like a simple end-to-end concatenation into a longer 1D array, since there's no second axis to stack along — this special-case behavior for 1D inputs is a common point of confusion, since it doesn't parallel vstack()'s behavior of promoting 1D arrays into rows.

1Understanding np.hstack()

For 2D arrays, hstack() places inputs side by side, requiring them to have the same number of rows, equivalent to np.concatenate(..., axis=1). For 1D arrays specifically, hstack() behaves like a simple end-to-end concatenation into a longer 1D array, since there's no second axis to stack along — this special-case behavior for 1D inputs is a common point of confusion, since it doesn't parallel vstack()'s behavior of promoting 1D arrays into rows.

💡

Don't assume hstack() and vstack() are perfect mirror images for 1D inputs — vstack() turns two 1D arrays into a 2D result, stacked as rows, while hstack() on the same two 1D arrays just concatenates them into one longer 1D array.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.hstack((a, b)))
localhost:3000

2Practical Example

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

editor.html
import numpy as np

a = np.array([[1], [2]])
b = np.array([[3], [4]])
print(np.hstack((a, b)))
localhost:3000

3Best Practices

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

1. Use hstack() for the common case of joining 2D arrays side by side by columns, instead of the more generic np.concatenate(..., axis=1)

2. Remember hstack() on 1D arrays produces a longer 1D array, not a 2D result, unlike the analogous vstack() call

3. Ensure every input array has the same number of rows, matching size along axis 0, before calling hstack() on 2D arrays

⚠️

Tip: Don't assume hstack() and vstack() are perfect mirror images for 1D inputs — vstack() turns two 1D arrays into a 2D result, stacked as rows, while hstack() on the same two 1D arrays just concatenates them into one longer 1D array.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.hstack((a, b)))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.hstack((a, b)))
Example 02Advanced Example
import numpy as np

a = np.array([[1], [2]])
b = np.array([[3], [4]])
print(np.hstack((a, b)))

Best Practices

  • Use hstack() for the common case of joining 2D arrays side by side by columns, instead of the more generic np.concatenate(..., axis=1)
  • Remember hstack() on 1D arrays produces a longer 1D array, not a 2D result, unlike the analogous vstack() call
  • Ensure every input array has the same number of rows, matching size along axis 0, before calling hstack() on 2D arrays

Interview Question

Why does np.hstack() on two 1D arrays behave differently from np.vstack() on the same two arrays, given their names suggest they're mirror images?

Hint: Think about what 'horizontal' and 'vertical' actually mean once an array only has one dimension.

vstack() always treats each input as a row and stacks those rows into a new 2D result, even for 1D inputs, giving a genuinely two-dimensional output. hstack(), for 1D inputs specifically, has no second axis to place things side by side along, so it falls back to simple end-to-end concatenation into a single, longer 1D array instead of creating a 2D result. The two functions are true mirror images only once the inputs are already 2D or higher; for plain 1D arrays, their behavior diverges in a way that surprises people expecting perfect symmetry.

Exercises

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

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

Frequently Asked Questions

Why does np.hstack() on two 1D arrays behave differently from np.vstack() on the same two arrays, given their names suggest they're mirror images?

vstack() always treats each input as a row and stacks those rows into a new 2D result, even for 1D inputs, giving a genuinely two-dimensional output. hstack(), for 1D inputs specifically, has no second axis to place things side by side along, so it falls back to simple end-to-end concatenation into a single, longer 1D array instead of creating a 2D result. The two functions are true mirror images only once the inputs are already 2D or higher; for plain 1D arrays, their behavior diverges in a way that surprises people expecting perfect symmetry.

Related Functions

np-vstacknp-stacknp-concatenate