---
title: "How to use NumPy Joining Array?"  
description: "How to use NumPy Joining Array?"  
author: "Ravi Vishwakarma"  
published: 2025-11-13  
updated: 2025-11-13  
canonical: https://www.mindstick.com/interview/34412/how-to-use-numpy-joining-array  
category: "python"  
tags: ["python-3.4", "numpy"]  
reading_time: 4 minutes  

---

# How to use NumPy Joining Array?

> Easy guide to **joining** [**arrays in NumPy**](https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing) (combining two or more arrays into one).\
> NumPy provides multiple ways to join arrays depending on the shape and how you want to merge them.

## 1. Using `np.concatenate()`

This is the most common way to join arrays.

### Example — Join 1D Arrays

```python
import numpy as np

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

result = np.concatenate((a, b))
print(result)
```

## Output:

```plaintext
[1 2 3 4 5 6]
```

### Example — Join 2D Arrays (Row-wise)

```python
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

result = np.concatenate((a, b), axis=0)
print(result)
```

## Output:

```plaintext
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
```

### Example — Join 2D Arrays (Column-wise)

```python
result = np.concatenate((a, b), axis=1)
print(result)
```

## Output:

```plaintext
[[1 2 5 6]
 [3 4 7 8]]
```

## 2. Using `np.stack()`

Creates a **new dimension**.

### Example:

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

result = np.stack((a, b), axis=0)
print(result)
```

## Output:

```plaintext
[[1 2 3]
 [4 5 6]]
```

Here, arrays are stacked into 2 rows.

## 3. Using `np.hstack()` (Horizontal Stack)

Joins arrays **side by side**.

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

result = np.hstack((a, b))
print(result)
```

## Output:

```plaintext
[1 2 3 4 5 6]
```

For 2D arrays, it stacks column-wise.

## 4. Using `np.vstack()` (Vertical Stack)

Joins arrays **top to bottom**.

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

result = np.vstack((a, b))
print(result)
```

## Output:

```plaintext
[[1 2 3]
 [4 5 6]]
```

## 5. Using `np.dstack()` (Depth Stack)

Stacks arrays into **depth (3rd dimension)**.

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

result = np.dstack((a, b))
print(result)
```

## Output:

```plaintext
[[[1 4]
  [2 5]
  [3 6]]]
```

## Summary Table

| Function | Behavior |
| --- | --- |
| `concatenate()` | Join along existing axis |
| `stack()` | Join but create a new axis |
| `hstack()` | Join horizontally |
| `vstack()` | Join vertically |
| `dstack()` | Join depth-wise (3D) |

## Answers

### Answer by Ravi Vishwakarma

> Easy guide to **joining** [**arrays in NumPy**](https://www.mindstick.com/interview/34403/how-to-access-numpy-array-indexing) (combining two or more arrays into one).\
> NumPy provides multiple ways to join arrays depending on the shape and how you want to merge them.

## 1. Using `np.concatenate()`

This is the most common way to join arrays.

### Example — Join 1D Arrays

```python
import numpy as np

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

result = np.concatenate((a, b))
print(result)
```

## Output:

```plaintext
[1 2 3 4 5 6]
```

### Example — Join 2D Arrays (Row-wise)

```python
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])

result = np.concatenate((a, b), axis=0)
print(result)
```

## Output:

```plaintext
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
```

### Example — Join 2D Arrays (Column-wise)

```python
result = np.concatenate((a, b), axis=1)
print(result)
```

## Output:

```plaintext
[[1 2 5 6]
 [3 4 7 8]]
```

## 2. Using `np.stack()`

Creates a **new dimension**.

### Example:

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

result = np.stack((a, b), axis=0)
print(result)
```

## Output:

```plaintext
[[1 2 3]
 [4 5 6]]
```

Here, arrays are stacked into 2 rows.

## 3. Using `np.hstack()` (Horizontal Stack)

Joins arrays **side by side**.

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

result = np.hstack((a, b))
print(result)
```

## Output:

```plaintext
[1 2 3 4 5 6]
```

For 2D arrays, it stacks column-wise.

## 4. Using `np.vstack()` (Vertical Stack)

Joins arrays **top to bottom**.

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

result = np.vstack((a, b))
print(result)
```

## Output:

```plaintext
[[1 2 3]
 [4 5 6]]
```

## 5. Using `np.dstack()` (Depth Stack)

Stacks arrays into **depth (3rd dimension)**.

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

result = np.dstack((a, b))
print(result)
```

## Output:

```plaintext
[[[1 4]
  [2 5]
  [3 6]]]
```

## Summary Table

| Function | Behavior |
| --- | --- |
| `concatenate()` | Join along existing axis |
| `stack()` | Join but create a new axis |
| `hstack()` | Join horizontally |
| `vstack()` | Join vertically |
| `dstack()` | Join depth-wise (3D) |


---

Original Source: https://www.mindstick.com/interview/34412/how-to-use-numpy-joining-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
