Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to get the subarray from an array using a specified range of indices
An array is a homogeneous data structure used to store elements of the same data type. Each element is identified by a key or index value. Python provides several ways to extract subarrays from arrays using specified index ranges.
What is a Subarray?
A subarray is a continuous portion of elements from an array. For example, if we have an array:
numbers = [9, 3, 1, 6, 9]
print("Original array:", numbers)
Original array: [9, 3, 1, 6, 9]
The possible subarrays include:
[9, 3] [9, 3, 1] [3, 1, 6, 9]
Arrays in Python
Python offers three main approaches to work with arrays:
Lists Native Python data structure:
[1, 2, 3, 4, 5]Array module Typed arrays:
array('i', [1, 2, 3, 4])NumPy arrays Scientific computing:
array([1, 2, 3, 4])
All these arrays are indexed from 0 to (n-1). We'll use Python's slicing feature to extract subarrays.
Slicing Syntax
Python slicing follows this syntax:
iterable_obj[start:stop:step]
Where:
Start Starting index (default: 0)
Stop Ending index (exclusive, default: length of object)
Step Increment value (default: 1)
Using List Slicing
Lists support slicing to extract subarrays using list[start:end:step] syntax:
# Creating array
data = [2, 5, 6, 7, 1, 9, 2, 0]
print("The original array is:", data)
# Get subarray from index 1 to 7
result = data[1:7]
print("The subarray is:", result)
The original array is: [2, 5, 6, 7, 1, 9, 2, 0] The subarray is: [5, 6, 7, 1, 9, 2]
Using NumPy Arrays
NumPy arrays also support slicing with the same syntax. The slicing returns a view of the original array:
import numpy as np
# Creating NumPy array
numbers = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print("The original array is:", numbers)
# Get subarray from index 1 to 4
result = numbers[1:4]
print("The subarray is:", result)
The original array is: [1 2 3 4 5 6 7 8 9] The subarray is: [2 3 4]
Using Array Module
The array module provides typed arrays that also support slicing operations:
import array
# Creating array with integer type
data = array.array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
print("The original array is:", data)
# Get subarray from index 1 to 8
result = data[1:8]
print("The subarray is:", result)
The original array is: array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9])
The subarray is: array('i', [2, 3, 4, 5, 6, 7, 8])
Comparison
| Method | Memory Usage | Performance | Best For |
|---|---|---|---|
| List Slicing | Creates copy | Good | General purpose |
| NumPy Arrays | Creates view | Excellent | Numerical computing |
| Array Module | Creates copy | Good | Memory-efficient storage |
Conclusion
Python slicing provides a uniform way to extract subarrays across different array types. Use NumPy for numerical operations and large datasets, while lists work well for general programming tasks.
