What does [:, :] mean on NumPy arrays?

Learn about the meaning of [:, :] in NumPy arrays.
By Pranit Sharma Last updated : December 25, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

Use of [:, :] in NumPy Arrays

Basically, [: , :] stands for everything from the beginning to the end just like for lists. The first colon stands for the first dimension and the second colon is for the second dimension.

If we use the second colon alone and specify a value for the first colon, it will work for all the rows but only for the specific column.

If we use the first colon alone and specify a value for the second colon, it will work for all the columns but only for the specific row.

Let us understand with the help of an example,

Python code to demonstrate the use of [:, :] in NumPy arrays

# Import numpy
import numpy as np

# Creating a numpy array
arr = np.zeros((3, 3))

# Display original image
print("Original Array:\n",arr,"\n")

# working on all rows but a specific column
arr[1, :] = 3

# Display result
print("Result:\n",arr,"\n")

# working on all columns but a specific row
arr[:, 2] = 1

# Display result
print("Result:\n",arr,"\n")

Output

Example: What does [:, :] mean on NumPy arrays?

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.