LibraryArray indexing and slicing

Array indexing and slicing

Learn about Array indexing and slicing as part of Python Data Science and Machine Learning

Mastering Array Indexing and Slicing in Python

In Python, especially when working with libraries like NumPy for data science, understanding how to access and manipulate elements within arrays is fundamental. This involves two key concepts: indexing and slicing.

Array Indexing: Accessing Individual Elements

Indexing allows you to retrieve a single element from an array using its position. Python arrays (and lists) are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on. Negative indexing is also supported, where -1 refers to the last element, -2 to the second-to-last, etc.

What is the index of the first element in a Python array?

0

How do you access the last element of an array using negative indexing?

Using index -1

Array Slicing: Extracting Sub-Arrays

Slicing enables you to extract a portion (a sub-array or 'slice') of an array. The syntax for slicing is

code
[start:stop:step]
.

  • code
    start
    : The index where the slice begins (inclusive). If omitted, it defaults to the beginning of the array.
  • code
    stop
    : The index where the slice ends (exclusive). If omitted, it defaults to the end of the array.
  • code
    step
    : The increment between elements. If omitted, it defaults to 1.

Slicing extracts a range of elements from an array.

Slicing uses the [start:stop:step] notation to define the desired portion of an array. Remember that the stop index is exclusive.

Consider an array my_array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

  • my_array[2:5] would return [2, 3, 4] (elements from index 2 up to, but not including, index 5).
  • my_array[:3] would return [0, 1, 2] (elements from the beginning up to index 3).
  • my_array[5:] would return [5, 6, 7, 8, 9] (elements from index 5 to the end).
  • my_array[1:7:2] would return [1, 3, 5] (elements from index 1 to 7, taking every second element).

Visualizing array slicing helps solidify understanding. Imagine an array as a sequence of boxes. Indexing picks out a single box. Slicing selects a contiguous group of boxes, or a group with gaps if a step is specified. The start index points to the first box to include, and the stop index points to the box after the last one to include. The step determines how many boxes to skip between selections.

📚

Text-based content

Library pages focus on text content

Key takeaway: Indexing gets one item, slicing gets a range of items. Always remember the stop index is exclusive!

Multi-dimensional Arrays

For multi-dimensional arrays (like matrices), indexing and slicing become more complex. You use multiple indices separated by commas. For example,

code
my_matrix[row_index, column_index]
accesses a single element. Slicing can also be applied to each dimension independently:
code
my_matrix[row_start:row_stop, col_start:col_stop]
.

How would you access the element in the 3rd row and 2nd column of a 2D array (using 0-based indexing)?

array[2, 1]

Practical Application with NumPy

NumPy arrays are the backbone of numerical computation in Python. Their efficient indexing and slicing capabilities are crucial for data manipulation, feature selection, and many machine learning algorithms.

Learning Resources

NumPy Official Documentation: Indexing and Slicing(documentation)

The definitive guide to NumPy's powerful indexing and slicing capabilities, covering both basic and advanced techniques.

Python Official Tutorial: Indexing and Slicing(documentation)

Learn the fundamentals of indexing and slicing as applied to Python lists, which share similar concepts with NumPy arrays.

Real Python: NumPy Array Indexing and Slicing(blog)

A comprehensive and practical tutorial with clear examples on how to use indexing and slicing with NumPy arrays.

DataCamp: NumPy Fundamentals(tutorial)

This course covers NumPy essentials, including detailed sections on array indexing and slicing within a data science context.

Towards Data Science: Mastering NumPy Slicing(blog)

An in-depth article exploring advanced slicing techniques and common pitfalls when working with NumPy arrays.

GeeksforGeeks: NumPy Array Indexing(blog)

Provides a clear explanation and code examples for accessing elements and sub-arrays in NumPy, including multi-dimensional arrays.

YouTube: NumPy Indexing and Slicing Explained(video)

A visual explanation of NumPy indexing and slicing concepts, demonstrating how to extract data efficiently.

Kaggle Learn: NumPy(tutorial)

A short, interactive course on NumPy that includes practical exercises on array manipulation, indexing, and slicing.

Stack Overflow: NumPy slicing questions(documentation)

A community-driven resource for finding answers to specific questions and common issues related to NumPy indexing and slicing.

Wikipedia: Array slicing(wikipedia)

Provides a general overview of array slicing as a concept in computer programming, with examples from various languages.