Python Program to get the subarray from an array using a specified range of indices


An array is a homogeneous data structure, which is used to store a set of elements of the same data type. And each element in the array is identified by key or index value.

Subarray

A subarray is defined as a small part of continuous elements of an array. if we take an array of 5 integer elements like below.

[9, 3, 1, 6, 9]

Then the subarrays will be −

[9, 3]
[9, 3, 1] 
[3, 1, 6, 9]

Arrays in Python

In Python we don't have a specific data structure to represent arrays. However, we can use Lists to represent arrays.

 [1, 2, 3, 4, 5]

Also we can use array or NumPy modules to work with arrays in python.

An array defined by the array module.

array('i', [1, 2, 3, 4])

A Numpy array defined by the NumPy module.

array([1, 2, 3, 4])

The above all arrays are indexed from starting 0 to (n-1).

In this article, we see multiple ways to get the subarray from an array using a specified range of indices

Input Output Scenarios

Assume we have an input array with 9 integer values. And in output will be the subarray from index 1 to 4.

Input array:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
[2, 3, 4]

Mainly we will use python slicing feature to get the subarray from an array. A slicing used to select a range of items of an iterable.

Syntax

iterable_obj[start:stop:step]

Using List Slicing

We can use list slicing to get the subarray. Following is the syntax to do so −

list_object[start : end : step]

Where,

  • Start  The starting index where slicing of the list starts. The default value is 0.

  • End  The ending index where slicing of the list stops. Note that the value of this index is not a part of the end result. The default value is the length of the iterable object.

  • Step  By default, it is 1. The number to increment the starting index.

Example

From the following example, we can see an array and two indices arr[1:7] are given to pick subarrays from those locations.

# creating array
arr = [2, 5, 6, 7, 1, 9, 2, 0]
print ("The original array is: ", arr) 
print() 
# get the subarray 
result = arr[1:7]
print ("The subarray is: ", result) 

Output

The original array is:  [2, 5, 6, 7, 1, 9, 2, 0]
The subarray is:  [5, 6, 7, 1, 9, 2]

Using a NumPy Array

In Numpy array slicing allows us to specify a range of indexes to get the subarray from a bigger array. The NumPy array elements are also indexed like a python list. The index of the first element will be 0 and the last element will be indexed n-1.

Syntax

numpyArray[start : end]

Where,

  • Start  The starting index where slicing of the array starts.

  • End  The ending index where slicing of the array stops.

Example

Following is another example –

import numpy as np
# creating array
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print ("The original array is: ", arr) 
print() 
# get the subarray 
result = arr[1:4]
print ("The subarray is: ", result) 

Output

The original array is:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
The subarray is:  [2, 3, 4]

We successfully got a subarray starting from the 1st index up to the 4th index (excluding the last the 4th index) was selected.

Use the Array Module

The python array module also supports array slicing, so that we can easily get the subarray from a big array. Following is the syntax –

Syntax

array[start : end]

Where,

  • Start  The starting index where slicing of the array starts.

  • End  The ending index where the slicing of the array stops.

Example

In the following example, we have successfully got the subarray from index 1- 8 using the python array module and its slicing technique.

import array
# creating array
arr = array.array('i',[1, 2, 3, 4, 5, 6, 7, 8, 9])
print ("The original array is: ", arr) 
print() 

# get the subarray 
result = arr[1:8]
print ("The subarray is: ", result) 

Output

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])

Updated on: 29-May-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements