The array slicing technique is used to access the specific portion or a sequence of an element from the arrays. Array slicing techniques provide several features, such as performing various operations on arrays, manipulating data more efficiently or extracting useful information, etc.
This post provides a complete guide on array slicing using the below contents:
- How to Slice an Array in Python?
- With Stop Parameter
- With Start and Stop Parameters
- With the Step Parameter
- With the slice() Method
- Array Slicing of 2-D and 3-D Array
- Negative Array Slicing
How to Slice an Array in Python?
Slicing an array means taking the part of the array according to the specified index value. The following syntax is used to slice an index:
Syntax:
array[start:stop:step]
The above syntax specifies which part of the array we want to slice. For instance, array[3:4] will give the elements from the index “3” to index “4”. The step parameter is used to add the steps between the start and stop numbers of the sequence.
Note: If we do not specify the start parameter value and the end parameter value, such as “[:4]”, then the slicing will start from the start parameter. In another case, If the end parameter is not mentioned, such as “[3:],” then the slicing of the array starts from the start and is sliced till the end of the array.
Let’s see different examples of array slicing:
Example 1: Array Slicing With Stop Parameter
The following example is used to slice an array using only the stop parameter value:
Code:
import array
array_value= array.array('i',[32, 15, 19, 22, 55])
print("Original array: ", array_value)
print("\nSliced array: ", array_value[:3])
- The module named “array” is imported.
- The “array.array()” function creates the array by taking the integer and array value as an argument.
- The expression “[:3]” slicing stop parameter is used to slice the array from the start to the stop index position.
Output:
The input array has been sliced.
Example 2: Array Slicing With Start and Stop Parameters
The below examples are used to slice the input array using the start and stop parameter value:
Code:
import numpy
array_value= numpy.array([32, 15, 19, 22, 55])
print("Original array: ", array_value)
print("\nSliced array: ", array_value[1:3])
- The “numpy.array()” function of the numpy array module is used to create the array by taking the value of the array element.
- The start and stop parameter is used in the array slicing expression “[1:3]” to slice the given array from the start index to the end index.
Output:
The given array has been sliced successfully.
Example 3: Array Slicing With the Step Parameter
The below code is used to slice the input array using the step parameter value along with a start and stop:
Code:
import numpy
array_value= numpy.array([32, 15, 19, 22, 55, 43, 25, 11])
print("Original array: ", array_value)
print("\nSliced array: ", array_value[2:7:2])
- The “numpy.array()” function creates the array of specific elements.
- The start, stop, and step parameter is used in the array slicing expression “[2:7:2]”. This expression means that the array is going to be sliced from the index “2” to the index “7” with a step value of “2”.
Output:
The original array has been sliced based on the start, stop, and step values.
Example 4: Slicing With the slice() Method
In Python, the “slice()” method retrieves a part/portion of an iterable such as a string, list, tuple, etc., based on the specified range.
The following example uses this method to slice an array:
Code:
import numpy
array_value= numpy.array([32, 15, 19, 22, 55, 43, 25, 11])
print("Original array: ", array_value)
slice_array = slice(1,6,2)
print("\nSliced array: ", array_value[slice_array])
- The “numpy.array()” function is used to generate the numpy array.
- The slice() method is utilized for slicing the array.
- The start, stop, and step value is passed as a parameter to the slice() method.
- The array sequence is passed to the array using square bracket notation and retrieves the sliced array.
Output:
The original array has been sliced.
Example 5: Array Slicing of 2-D and 3-D Array
The below example is used to slice a 2-D and 3-D array:
Code:
import numpy
array_value = numpy.array([[22, 32, 15, 17],
[22, 55, 25, 32]])
print(array_value[0, 1:3])
- The “numpy.array()” function is used to create the 2-dimensional array.
- After that, we print a slice of the array using indexing notation
- In the string slicing expression “array_value[0, 1:3],” the first parameter specifies the row (0), and the second parameter “1:3” indicates the elements from index one to index two.
Output:
The slice of the array has been printed successfully.
After the slicing of the 2-d array, the below code is used to slice a 3-D array:
Code:
import numpy
array_value = numpy.array([[[45, 55, 34, 15], [45, 15, 32, 44]],
[[1, 5, 6, 7],[22, 32, 15, 17]]])
print(array_value[0, 1, 1:4])
- The module named “numpy” is imported.
- The “numpy.array()” function is used to create the “3-dimensional” nested array with two rows and two columns.
- The expression “[0, 1, 1:4]” is used to slice the original array.
- The “0” in the expression specifies the selection of the first row placed at index 0.
- Similarly, the “1” in the expression specifies the selection of the second column placed at index “1” of that row.
- Lastly, the “1:4” range of index specifies the selection of the elements from index 1 to index 3 (excluding index 4) of that column.
Output:
The slice of the array using indexing has been printed.
Example 6: Negative Array Slicing
The negative value can be used to slice the array from the end side. The below code demonstrates the working of negative array slicing in Python:
Code:
import array
array_value= array.array('i',[32, 15, 19, 22, 55])
print("Original array: ", array_value)
print("\nSliced array: ", array_value[-3:-1])
- The module named “array” is imported.
- The “array.array()‘ function of the array module is utilized to create the array.
- The “negative slicing” value is passed using the “-” minus operator.
- The expression “[-3:-1]” will slice the array from index 3 to index 1 from the end side.
Output:
The given number has been sliced from the end side using negative indexing.
Conclusion
The slicing syntax “array[start:stop:step]” and the “slice()” function is used to slice an array based on the specified index. The array slicing is performed for the specific index using the start, stop, and step parameters. The “slice()” method is also used to slice the input array based on the index. The array slicing can also be performed on 2-D and 3-D arrays using indexing. This guide presented an in-depth guide on array slicing in Python.