Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Slicing | Indexing and Slicing
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookSlicing

Slicing in Python refers to retrieving elements from one index to another within a sequence. In this chapter, however, we will focus on slicing in NumPy arrays.

Slicing in 1D Arrays

The general syntax for slicing in 1D arrays is as follows: array[start:end:step]. Let’s take a more detailed look at it:

  • start is the index at which to start slicing;
  • end is the index at which slicing ends (the index itself is not included);
  • step specifies the increments between the indices (default is 1).

Here is an example to clarify everything (purple squares represent the elements retrieved from slicing):

Note

Since we did not explicitly specify step, it defaults to a value of 1.

Here is the code for this example:

123456789101112
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the element at index 2 to the element at index 4 exclusive print(array[2:4]) print('-' * 47) # Slicing from the first element to the element at index 5 exclusive print(array[:5]) print('-' * 47) # Slicing from the element at index 5 to the last element inclusive print(array[5:])
copy

Omitting Start, End, and Step

As you can see, we can often omit the start, end, step, or even all of them at the same time. For example, step can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:

    • Slicing from the first element (step is positive);
    • Slicing from the last element (step is negative).
  2. Omitting end:

    • Slicing to the last element inclusive (step is positive);
    • Slicing to the first element inclusive (step is negative).

Let's take a look at a few more examples (the black arrow indicates that the elements are taken in reverse order):

Here is the corresponding code:

123456789101112131415
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the first element to the last element inclusive with step=2 print(array[::2]) print('-' * 47) # Slicing from the element at index 4 to the element at index 2 exclusive (step=-1) print(array[4:2:-1]) print('-' * 47) # Slicing from the last element to the first element inclusive (reversed array) print(array[::-1]) print('-' * 47) # Slicing from the first element to the last inclusive (the same as our array) print(array[:])
copy
Task
test

Swipe to show code editor

You are analyzing the daily sales data of a small retail store. The sales for the past week are stored in a NumPy array, with each element representing the sales for a specific day. Create a slice of weekly_sales containing the sales data for every second day, starting from the second day (Tuesday), and store it in alternate_day_sales (use a positive index for start and do not specify end).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
toggle bottom row

bookSlicing

Slicing in Python refers to retrieving elements from one index to another within a sequence. In this chapter, however, we will focus on slicing in NumPy arrays.

Slicing in 1D Arrays

The general syntax for slicing in 1D arrays is as follows: array[start:end:step]. Let’s take a more detailed look at it:

  • start is the index at which to start slicing;
  • end is the index at which slicing ends (the index itself is not included);
  • step specifies the increments between the indices (default is 1).

Here is an example to clarify everything (purple squares represent the elements retrieved from slicing):

Note

Since we did not explicitly specify step, it defaults to a value of 1.

Here is the code for this example:

123456789101112
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the element at index 2 to the element at index 4 exclusive print(array[2:4]) print('-' * 47) # Slicing from the first element to the element at index 5 exclusive print(array[:5]) print('-' * 47) # Slicing from the element at index 5 to the last element inclusive print(array[5:])
copy

Omitting Start, End, and Step

As you can see, we can often omit the start, end, step, or even all of them at the same time. For example, step can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:

    • Slicing from the first element (step is positive);
    • Slicing from the last element (step is negative).
  2. Omitting end:

    • Slicing to the last element inclusive (step is positive);
    • Slicing to the first element inclusive (step is negative).

Let's take a look at a few more examples (the black arrow indicates that the elements are taken in reverse order):

Here is the corresponding code:

123456789101112131415
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the first element to the last element inclusive with step=2 print(array[::2]) print('-' * 47) # Slicing from the element at index 4 to the element at index 2 exclusive (step=-1) print(array[4:2:-1]) print('-' * 47) # Slicing from the last element to the first element inclusive (reversed array) print(array[::-1]) print('-' * 47) # Slicing from the first element to the last inclusive (the same as our array) print(array[:])
copy
Task
test

Swipe to show code editor

You are analyzing the daily sales data of a small retail store. The sales for the past week are stored in a NumPy array, with each element representing the sales for a specific day. Create a slice of weekly_sales containing the sales data for every second day, starting from the second day (Tuesday), and store it in alternate_day_sales (use a positive index for start and do not specify end).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
toggle bottom row

bookSlicing

Slicing in Python refers to retrieving elements from one index to another within a sequence. In this chapter, however, we will focus on slicing in NumPy arrays.

Slicing in 1D Arrays

The general syntax for slicing in 1D arrays is as follows: array[start:end:step]. Let’s take a more detailed look at it:

  • start is the index at which to start slicing;
  • end is the index at which slicing ends (the index itself is not included);
  • step specifies the increments between the indices (default is 1).

Here is an example to clarify everything (purple squares represent the elements retrieved from slicing):

Note

Since we did not explicitly specify step, it defaults to a value of 1.

Here is the code for this example:

123456789101112
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the element at index 2 to the element at index 4 exclusive print(array[2:4]) print('-' * 47) # Slicing from the first element to the element at index 5 exclusive print(array[:5]) print('-' * 47) # Slicing from the element at index 5 to the last element inclusive print(array[5:])
copy

Omitting Start, End, and Step

As you can see, we can often omit the start, end, step, or even all of them at the same time. For example, step can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:

    • Slicing from the first element (step is positive);
    • Slicing from the last element (step is negative).
  2. Omitting end:

    • Slicing to the last element inclusive (step is positive);
    • Slicing to the first element inclusive (step is negative).

Let's take a look at a few more examples (the black arrow indicates that the elements are taken in reverse order):

Here is the corresponding code:

123456789101112131415
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the first element to the last element inclusive with step=2 print(array[::2]) print('-' * 47) # Slicing from the element at index 4 to the element at index 2 exclusive (step=-1) print(array[4:2:-1]) print('-' * 47) # Slicing from the last element to the first element inclusive (reversed array) print(array[::-1]) print('-' * 47) # Slicing from the first element to the last inclusive (the same as our array) print(array[:])
copy
Task
test

Swipe to show code editor

You are analyzing the daily sales data of a small retail store. The sales for the past week are stored in a NumPy array, with each element representing the sales for a specific day. Create a slice of weekly_sales containing the sales data for every second day, starting from the second day (Tuesday), and store it in alternate_day_sales (use a positive index for start and do not specify end).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Slicing in Python refers to retrieving elements from one index to another within a sequence. In this chapter, however, we will focus on slicing in NumPy arrays.

Slicing in 1D Arrays

The general syntax for slicing in 1D arrays is as follows: array[start:end:step]. Let’s take a more detailed look at it:

  • start is the index at which to start slicing;
  • end is the index at which slicing ends (the index itself is not included);
  • step specifies the increments between the indices (default is 1).

Here is an example to clarify everything (purple squares represent the elements retrieved from slicing):

Note

Since we did not explicitly specify step, it defaults to a value of 1.

Here is the code for this example:

123456789101112
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the element at index 2 to the element at index 4 exclusive print(array[2:4]) print('-' * 47) # Slicing from the first element to the element at index 5 exclusive print(array[:5]) print('-' * 47) # Slicing from the element at index 5 to the last element inclusive print(array[5:])
copy

Omitting Start, End, and Step

As you can see, we can often omit the start, end, step, or even all of them at the same time. For example, step can be omitted when we want it to be equal to 1. start and end can be omitted in the following scenarios:

  1. Omitting start:

    • Slicing from the first element (step is positive);
    • Slicing from the last element (step is negative).
  2. Omitting end:

    • Slicing to the last element inclusive (step is positive);
    • Slicing to the first element inclusive (step is negative).

Let's take a look at a few more examples (the black arrow indicates that the elements are taken in reverse order):

Here is the corresponding code:

123456789101112131415
import numpy as np array = np.array([5, 10, 2, 8, 9, 1, 0, 4]) print(f'Initial array: {array}') print('-' * 47) # Slicing from the first element to the last element inclusive with step=2 print(array[::2]) print('-' * 47) # Slicing from the element at index 4 to the element at index 2 exclusive (step=-1) print(array[4:2:-1]) print('-' * 47) # Slicing from the last element to the first element inclusive (reversed array) print(array[::-1]) print('-' * 47) # Slicing from the first element to the last inclusive (the same as our array) print(array[:])
copy
Task
test

Swipe to show code editor

You are analyzing the daily sales data of a small retail store. The sales for the past week are stored in a NumPy array, with each element representing the sales for a specific day. Create a slice of weekly_sales containing the sales data for every second day, starting from the second day (Tuesday), and store it in alternate_day_sales (use a positive index for start and do not specify end).

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt