Slice an array Python
In this post, you will learn how to slice an array using the Python programming language.
An array in Python is a special variable, which can hold more than one value at a time. An array is defined as a collection of items that are stored at contiguous memory locations.
NumPy is the fundamental package for scientific computing with Python. It is a highly optimised library for numerical operations. The support of NumPy makes the task easier.
The Python NumPy module provides the array() function to create the numpy array. This function accepts lists of element values inside a square bracket. We can easily create a single or multi-dimensional array using this method. We can also specify the data type of the array elements.
In programming, array slicing is an operation that extracts a subset of elements from one given index to another given index of an array. To get a slice of an array, the colon operator : is used.
Syntax-<slice> = <array>[start:stop]
Where <slice> is the slice or section of the array object <array>. The index of the slice is specified in [start:stop].
Example1 of Python array slicing
import numpy as np
arr = np.array([4, 6, 9, 12, 43, 53, 69, 73])
# slicing from 1 to 6
sub_arr = arr[1:6]
print(sub_arr)
[ 6 9 12 43 53]
Example2 of Python array slicing
import numpy as np
arr = np.array([4, 6, 9, 12, 43, 53, 69, 73])
# Slice Starting from 4th value to end
sub_arr = arr[4:]
print(sub_arr)
[43 53 69 73]
Example3 of Python array slicing
import numpy as np
arr = np.array([4, 6, 9, 12, 43, 53, 69, 73])
# Slice from 0 to 4 index
sub_arr = arr[:5]
print(sub_arr)
[ 4 6 9 12 43]
Example4 of Python array slicing
import numpy as np
arr = np.array([4, 6, 9, 12, 43, 53, 69, 73])
# use negative slicing
sub_arr = arr[-4:-2]
print(sub_arr)
[43 53]
Example5 of Python array slicing
import numpy as np
arr = np.array([4, 6, 9, 12, 43, 53, 69, 73])
# use step value
sub_arr = arr[::5]
print(sub_arr)
[ 4 53]
Example6 of Python 2D array slicing
import numpy as np
arr = np.array([[5, 6, 2, 11, 8],
[7, 4, 2, 8, 10]])
# 2D array slicing
sub_arr = arr[1:,1:4]
print(sub_arr)
[[4 2 8]]
Example7 of Python 3D array slicing
import numpy as np
arr = np.array([[[4, 7, 1, 2, 9],
[1, 2, 6, 8, 9]],
[[6, 3, 2, 1, 9],
[6, 2, 3, 5, 8]]])
# 2D array slicing
sub_arr = arr[0,1,0:3]
print(sub_arr)
[2 4 6]
Related Articles
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module