---
title: "How to use NumPy Array Slicing?"  
description: "How to use NumPy Array Slicing?"  
author: "ICSM Computer"  
published: 2025-11-09  
updated: 2025-11-17  
canonical: https://www.mindstick.com/forum/161991/how-to-use-numpy-array-slicing  
category: "python"  
tags: ["python-3.4", "numpy", "Python 3"]  
reading_time: 2 minutes  

---

# How to use NumPy Array Slicing?

How to use [NumPy Array](https://www.mindstick.com/interview/34411/how-to-iterating-numpy-array) Slicing, [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with example.

## Replies

### Reply by Anubhav Sharma

> ## What is Slicing?
>
> Slicing lets you extract a [**portion** of an array](https://www.mindstick.com/forum/160732/chunk-arrays) — like substring slicing in Python.

## Syntax:

```plaintext
array[start : end : step]
```

- **start** → index to begin (inclusive)
- **end** → index to stop (exclusive)
- **step** → how many to skip

## 1. Slicing a 1-D Array

### Example:

```python
import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60])
```

### a) Basic slicing

```python
print(arr[1:4])
```

Output:

```plaintext
[20 30 40]
```

### b) Without start

```python
print(arr[:3])
```

Output:

```plaintext
[10 20 30]
```

### c) Without end

```python
print(arr[3:])
```

Output:

```plaintext
[40 50 60]
```

### d) With step

```python
print(arr[0:6:2])
```

Output:

```plaintext
[10 30 50]
```

### e) Reverse slicing

```python
print(arr[::-1])
```

Output:

```plaintext
[60 50 40 30 20 10]
```

## 2. Slicing a 2-D Array

```python
arr2 = np.array([[10, 20, 30],
                 [40, 50, 60],
                 [70, 80, 90]])
```

## a) Slice specific row

```python
print(arr2[1, :])   # Entire 2nd row
```

Output:

```plaintext
[40 50 60]
```

## b) Slice specific column

```python
print(arr2[:, 1])   # Entire 2nd column
```

Output:

```plaintext
[20 50 80]
```

## c) Slice a sub-matrix

```python
print(arr2[0:2, 1:3])
```

Output:

```plaintext
[[20 30]
 [50 60]]
```

This extracts rows 0–1 and columns 1–2.

## 3. Slicing With Steps (2D)

### Example: Every 2nd row and column

```python
print(arr2[::2, ::2])
```

Output:

```plaintext
[[10 30]
 [70 90]]
```

## 4. Slicing Higher-Dimensional Arrays (3D)

If you have a 3D array (like an image):

```python
arr3 = np.arange(27).reshape(3, 3, 3)
```

Get 1st "layer":

```python
print(arr3[0, :, :])
```

Get middle column of every layer:

```python
print(arr3[:, :, 1])
```

## Summary Cheatsheet

| Operation | Example | Meaning |
| --- | --- | --- |
| `a[start:end]` | `a[1:4]` | From 1 to 3 |
| `a[:end]` | `a[:3]` | From 0 to 2 |
| `a[start:]` | `a[2:]` | From 2 to end |
| `a[::step]` | `a[::2]` | Every 2nd element |
| `a[::-1]` | Reverse array |  |
| `a[row, col]` | `a[1,2]` | Element at row 1 col 2 |
| `a[rows, cols]` | `a[0:2, 1:3]` | Sub-matrix |


---

Original Source: https://www.mindstick.com/forum/161991/how-to-use-numpy-array-slicing

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
