Skip to main content

NumPy Slicing Arrays


In this tutorial you will learn:

  • NumPy Array Slicing
  • 1-D NumPy Array Slicing
  • 2-D NumPy Array Slicing

NumPy Array Slicing

We can also slice NumPy Array just like lists. When we slice the array in NumPy  the same data is returned but the order is different. It is actually a view of the original array, whereas in case of lists slicing return a whole new list object. In order slice an array we pass there are three different ways. The first way is to pass the starting and ending index in square brackets and another way is to pass the starting index, ending index and step. Not passing any starting value means that the starting index value is 0 and not passing the ending value means we want the data till the last index value. Step value is 1 by default if we don’t write it explicitly.

1-D NumPy Array Slicing

Slicing 1-D NumPy is similar to slicing lists. We need to mention the starting and ending index in square brackets with a “:” in between. Let’s have a look at some examples.

  1. import numpy as np
  2. nparr = np.array([6,8,5,3,6,0,4,9,8,2])
  3. print(" ** 1D NumPy Array Slicing ** ")
  4. print(nparr[2:5])

Slicing with step 2

  1. print(" ** 1D NumPy Array Slicing with step 2 ** ")
  2. print(nparr[3:6:2])

Slicing with no start index but ending index is 3

  1. print(" ** 1D NumPy Array Slicing with default start ** ")
  2. print(nparr[:3])

Slicing with no ending index but starting index is 3

  1. print(" ** 1D NumPy Array Slicing with default end ** ")
  2. print(nparr[3:])

2-D NumPy Array Slicing

For slicing 2D arrays we need to write the index of array and then after the ‘,’ we can slice that array. Let’s look at some examples.

In this example we are accessing the second array and then slicing it from index 2 to 5 (not included).

  1. nparr = np.array([[6,8,5,3,6],[0,4,9,8,2]])
  2. print(" ** 2D NumPy Array Slicing ** ")
  3. print(nparr[1, 2:5])

Accessing second array with 0 as starting index and 3 as ending index.

  1. print(" ** 2D NumPy Array Slicing with default start for 2nd dimension ** ")
  2. print(nparr[1,:3])

Accessing second array with 1 as starting index and default ending index.

  1. print(" ** 2D NumPy Array Slicing with default end for 2nd dimension ** ")
  2. print(nparr[1,1:])

Slicing both arrays with first array starting from 0 to 4 and next array slicing from 1 to 3.

  1. print(" ** 2D NumPy Array Slicing with start, end for both dimensions** ")
  2. print(nparr[0:4,1:3])

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.