Introduction
NumPy array indexing allows you to access individual elements from one-dimensional, two-dimensional, and multi-dimensional arrays. Understanding indexing is essential for data analysis, machine learning, and scientific computing. In this chapter, you’ll practice the most commonly used NumPy array indexing questions with complete solutions. NumPy Array Indexing practice questions with solutions help to build concepts.
1. Python Program to Access the First Element of a NumPy Array
Problem Statement
Write a Python program to print the first element of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
print(numbers[0])
Sample Output
10
Explanation
Array indexing starts from 0. Therefore, index 0 returns the first element.
Concepts Covered
- Array Indexing
- First Element
- Index Position
2. Python Program to Access the Last Element of a NumPy Array
Problem Statement
Write a Python program to print the last element of a NumPy array using negative indexing.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
print(numbers[-1])
Sample Output
50
Explanation
Negative indexing starts from the end of the array. The index -1 always represents the last element.
Concepts Covered
- Negative Indexing
- Last Element
3. Python Program to Access the Third Element of a NumPy Array
Problem Statement
Write a Python program to print the third element of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([5, 10, 15, 20, 25])
print(numbers[2])
Sample Output
15
Explanation
Since indexing begins with 0, the third element is located at index 2.
Concepts Covered
- Index Position
- One-Dimensional Array
4. Python Program to Access an Element from a Two-Dimensional Array
Problem Statement
Write a Python program to print the value 6 from the following array.
Python Solution
import numpy as np
numbers = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(numbers[1, 2])
Sample Output
6
Explanation
The first index represents the row, and the second index represents the column.
Concepts Covered
- Two-Dimensional Array
- Row and Column Indexing
5. Python Program to Access an Element from a Three-Dimensional Array
Problem Statement
Write a Python program to print the value 8 from a three-dimensional NumPy array.
Python Solution
import numpy as np
numbers = np.array([
[
[1, 2],
[3, 4]
],
[
[5, 6],
[7, 8]
]
])
print(numbers[1, 1, 1])
Sample Output
8
Explanation
For a three-dimensional array, indexing follows the order: array, row, column.
Concepts Covered
- Three-Dimensional Array
- Multi-Dimensional Indexing
6. Python Program to Change an Array Element
Problem Statement
Write a Python program to change the second element of a NumPy array to 100.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40])
numbers[1] = 100
print(numbers)
Sample Output
[ 10 100 30 40]
Explanation
You can modify an array element by assigning a new value using its index.
Concepts Covered
- Updating Array Elements
- Array Assignment
7. Python Program to Change an Element in a Two-Dimensional Array
Problem Statement
Write a Python program to replace the value 5 with 50.
Python Solution
import numpy as np
numbers = np.array([
[1, 2, 3],
[4, 5, 6]
])
numbers[1, 1] = 50
print(numbers)
Sample Output
[[ 1 2 3]
[ 4 50 6]]
Explanation
Use row and column indexes to update elements in a two-dimensional array.
Concepts Covered
- Updating 2D Arrays
- Row and Column Indexing
8. Python Program to Access Multiple Elements Using Indexes
Problem Statement
Write a Python program to print the first and last elements of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
print(numbers[0], numbers[-1])
Sample Output
10 50
Explanation
You can access multiple elements by using multiple index positions in the same statement.
Concepts Covered
- Positive Indexing
- Negative Indexing
9. Python Program to Access an Entire Row
Problem Statement
Write a Python program to print the second row of a two-dimensional array.
Python Solution
import numpy as np
numbers = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(numbers[1])
Sample Output
[40 50 60]
Explanation
Providing only the row index returns the complete row.
Concepts Covered
- Row Selection
- Two-Dimensional Arrays
10. Python Program to Access an Entire Column
Problem Statement
Write a Python program to print the first column of a two-dimensional array.
Python Solution
import numpy as np
numbers = np.array([
[10, 20, 30],
[40, 50, 60]
])
print(numbers[:, 0])
Sample Output
[10 40]
Explanation
The colon (:) selects all rows, while the column index selects the required column.
Concepts Covered
- Column Selection
- Array Indexing
11. Python Program to Access Specific Elements from a 2D NumPy Array
Problem Statement
Write a Python program to create a 4×4 NumPy array and display the following elements:
- First element
- Last element
- Element at second row and third column
- Element at fourth row and second column
Python Solution
import numpy as np
array = np.array([
[10, 20, 30, 40],
[50, 60, 70, 80],
[90, 100, 110, 120],
[130, 140, 150, 160]
])
print("Original Array:")
print(array)
print("\nFirst Element:", array[0, 0])
print("Last Element:", array[-1, -1])
print("Second Row, Third Column:", array[1, 2])
print("Fourth Row, Second Column:", array[3, 1])
Sample Output
Original Array:
[[ 10 20 30 40]
[ 50 60 70 80]
[ 90 100 110 120]
[130 140 150 160]]
First Element: 10
Last Element: 160
Second Row, Third Column: 70
Fourth Row, Second Column: 140
Explanation
The program demonstrates how to access elements using row and column indices in a 2D NumPy array.
Concepts Covered
- 2D Array Indexing
- Positive Indexing
- Negative Indexing
12. Python Program to Extract Entire Rows and Columns from a NumPy Array
Problem Statement
Write a Python program to extract:
- First row
- Last row
- Second column
- Fourth column
from a NumPy array.
Python Solution
import numpy as np
array = np.array([
[5, 10, 15, 20],
[25, 30, 35, 40],
[45, 50, 55, 60],
[65, 70, 75, 80]
])
print("Original Array:")
print(array)
print("\nFirst Row:")
print(array[0])
print("\nLast Row:")
print(array[-1])
print("\nSecond Column:")
print(array[:, 1])
print("\nFourth Column:")
print(array[:, 3])
Sample Output
First Row:
[ 5 10 15 20]
Last Row:
[65 70 75 80]
Second Column:
[10 30 50 70]
Fourth Column:
[20 40 60 80]
Explanation
The : operator selects all rows while specifying a column index extracts an entire column.
Concepts Covered
- Row Selection
- Column Selection
- Array Indexing
13. Python Program to Access Elements Using Negative Indexing
Problem Statement
Write a Python program to demonstrate negative indexing in a NumPy array by displaying:
- Last row
- Last column
- Second last row
- Second last column
Python Solution
import numpy as np
array = np.array([
[11, 22, 33],
[44, 55, 66],
[77, 88, 99]
])
print("Original Array:")
print(array)
print("\nLast Row:")
print(array[-1])
print("\nLast Column:")
print(array[:, -1])
print("\nSecond Last Row:")
print(array[-2])
print("\nSecond Last Column:")
print(array[:, -2])
Sample Output
Last Row:
[77 88 99]
Last Column:
[33 66 99]
Second Last Row:
[44 55 66]
Second Last Column:
[22 55 88]
Explanation
Negative indexing starts from the end of the array.
-1→ Last element-2→ Second last element
Concepts Covered
- Negative Indexing
- Rows
- Columns
- NumPy Arrays
14. Python Program to Replace Specific Elements Using Indexing
Problem Statement
Write a Python program to replace:
- First element with 100
- Last element with 500
- Center element with 999
using NumPy indexing.
Python Solution
import numpy as np
array = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
print("Original Array:")
print(array)
array[0, 0] = 100
array[-1, -1] = 500
array[1, 1] = 999
print("\nUpdated Array:")
print(array)
Sample Output
Updated Array:
[[100 20 30]
[ 40 999 60]
[ 70 80 500]]
Explanation
NumPy arrays are mutable, so individual elements can be modified using indexing.
Concepts Covered
- Updating Array Values
- Index Assignment
- Mutable Arrays
15. Python Program to Find the Maximum and Minimum Elements Using Index Positions
Problem Statement
Write a Python program to display:
- Maximum value
- Minimum value
- Position (index) of the maximum value
- Position (index) of the minimum value
Python Solution
import numpy as np
array = np.array([
[25, 18, 92],
[40, 75, 12],
[88, 56, 61]
])
print("Original Array:")
print(array)
max_value = np.max(array)
min_value = np.min(array)
max_position = np.unravel_index(np.argmax(array), array.shape)
min_position = np.unravel_index(np.argmin(array), array.shape)
print("\nMaximum Value:", max_value)
print("Maximum Position:", max_position)
print("\nMinimum Value:", min_value)
print("Minimum Position:", min_position)
Sample Output
Maximum Value: 92
Maximum Position: (0, 2)
Minimum Value: 12
Minimum Position: (1, 2)
Explanation
np.argmax()returns the flattened index of the maximum element.np.argmin()returns the flattened index of the minimum element.np.unravel_index()converts the flattened index into row and column coordinates.
Concepts Covered
- Array Indexing
argmax()argmin()unravel_index()- Finding Element Positions
Chapter Summary
In this chapter, you learned how to access and modify elements in one-dimensional, two-dimensional, and three-dimensional NumPy arrays. You also practiced positive indexing, negative indexing, row selection, column selection, and updating array values.
Key Takeaways
- NumPy indexing starts from
0. - Negative indexing accesses elements from the end of the array.
- Multi-dimensional arrays use row and column indexes.
- Three-dimensional arrays require three indexes.
- Array elements can be modified using index assignment.
- The
:operator selects all rows or columns. - Indexing is one of the most important NumPy concepts for data manipulation.
Frequently Asked Questions (FAQs)
1. What is array indexing in NumPy?
Array indexing is the process of accessing individual elements in a NumPy array using their index positions.
2. Does NumPy indexing start from 0?
Yes. Like Python lists, NumPy arrays use zero-based indexing.
3. What is negative indexing in NumPy?
Negative indexing accesses elements from the end of the array. For example, -1 returns the last element.
4. How do I access an element in a 2D NumPy array?
Use the syntax array[row, column]. For example, array[1, 2].
5. Can I modify array elements using indexing?
Yes. Simply assign a new value using the index, such as array[0] = 100.
6. How do I access an entire row in a NumPy array?
Use the row index only. For example, array[1] returns the second row.
7. How do I access an entire column in a NumPy array?
Use : for all rows and specify the column index. For example, array[:, 0] returns the first column.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

