# NumPy Advanced Indexing: Exercises, Practice, and Solutions

## NumPy Advanced Indexing [ 20 exercises with solution]

[** An editor is available at the bottom of the page to write and execute the scripts.** Go to the editor]

**1.** Write a NumPy program that creates a 2D NumPy array of random integers. Use boolean indexing to select all elements greater than a specified value.

Click me to see the sample solution

**2.** Write a NumPy program that creates a 1D NumPy array and uses integer array indexing to select a subset of elements based on an index array.

Click me to see the sample solution

**3.** Write a NumPy program that creates a 3D NumPy array and uses fancy indexing to select elements from specific rows and columns.

Click me to see the sample solution

**4.** Write a NumPy program that creates a 4D NumPy array and uses multi-dimensional indexing to select a subarray.

Click me to see the sample solution

**5.** Write a NumPy program that creates a 2D NumPy array of random floats and uses boolean indexing to select elements that satisfy multiple conditions (e.g., greater than 0.5 and less than 0.8).

Click me to see the sample solution

**6.** Write a NumPy program that creates a 2D NumPy array and uses integer indexing with broadcasting to select elements from specific rows and all columns.

Click me to see the sample solution

**7.** Write a NumPy program that creates a 3D NumPy array and use boolean indexing to select elements along one axis based on conditions applied to another axis.

Click me to see the sample solution

**8.** Write a NumPy program that creates a 2D NumPy array and uses a tuple of arrays to index and select a specific set of elements.

Click me to see the sample solution

**9.** Write a NumPy program that creates two 1D NumPy arrays and uses np.ix_ to perform cross-indexing on a 2D array.

Click me to see the sample solution

**10.** Write a NumPy program that creates a 2D NumPy array and uses boolean indexing to replace all elements that meet a certain condition with a specified value.

Click me to see the sample solution

**11.** Write a NumPy program that creates a 3D NumPy array and uses integer array indexing to select elements along specific axes.

Click me to see the sample solution

**12.** Write a NumPy program that creates a 2D NumPy array and uses a combination of boolean and integer indexing to select a subset of elements.

Click me to see the sample solution

**13.** Write a NumPy program that creates a 1D NumPy array and uses boolean indexing with logical operators (e.g., & for AND, | for OR) to select elements based on multiple conditions.

Click me to see the sample solution

**14.** Write a NumPy program that creates a 2D NumPy array and uses slicing in combination with index arrays to select a rectangular subarray.

Click me to see the sample solution

**15.** Write a NumPy program that creates a 3D NumPy array and uses boolean indexing to set elements that meet a condition to a new value.

Click me to see the sample solution

**16.** Write a NumPy program that creates a 2D NumPy array and uses np.nonzero to find indices of elements that satisfy a condition, then uses these indices for advanced indexing.

Click me to see the sample solution

**17.** Write a NumPy program that creates a 4D NumPy array and uses integer indexing to extract smaller subarrays.

Click me to see the sample solution

**18.** Write a Numpy program that creates a 3D NumPy array and use a combination of slicing and integer indexing to select a specific slice and then index into it.

Click me to see the sample solution

**19.** Write a NumPy program that creates a 5D NumPy array. Use boolean indexing to select elements along specific dimensions based on conditions applied to other dimensions.

Click me to see the sample solution

**20.** Write a NumPy program that creates a 2D NumPy array and uses a mask array (boolean array) for indexing to select a subset of elements that match the mask criteria.

Click me to see the sample solution

**Python-Numpy Code Editor:**

**More to Come !**

**Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.**

Test your Python skills with w3resource's quiz

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics