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
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.

