Introduction
NumPy array iteration is used to access each element of an array one by one. It is useful for processing data, performing calculations, and working with multi-dimensional arrays. In this chapter, you’ll practice the most important NumPy array iteration questions with complete solutions. NumPy Array Iteration practice questions with solutions help to understand the concepts.
1. Python Program to Iterate Through a One-Dimensional NumPy Array
Problem Statement
Write a Python program to print each element of a one-dimensional NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
for num in numbers:
print(num)
Sample Output
10
20
30
40
50
Explanation
A for loop accesses each element of the array one at a time.
Concepts Covered
- Array Iteration
- for Loop
- One-Dimensional Array
2. Python Program to Iterate Through a Two-Dimensional NumPy Array
Problem Statement
Write a Python program to print every element of a two-dimensional NumPy array.
Python Solution
import numpy as np
numbers = np.array([
[10, 20, 30],
[40, 50, 60]
])
for row in numbers:
for value in row:
print(value)
Sample Output
10
20
30
40
50
60
Explanation
Nested for loops are used to access every element in a two-dimensional array.
Concepts Covered
- Nested Loop
- Two-Dimensional Array
- Array Traversal
3. Python Program to Iterate Through a Three-Dimensional NumPy Array
Problem Statement
Write a Python program to print every element of a three-dimensional NumPy array.
Python Solution
import numpy as np
numbers = np.array([
[[1, 2], [3, 4]],
[[5, 6], [7, 8]]
])
for array in numbers:
for row in array:
for value in row:
print(value)
Sample Output
1
2
3
4
5
6
7
8
Explanation
Each additional dimension requires another nested loop.
Concepts Covered
- Three-Dimensional Array
- Nested Loops
- Array Iteration
4. Python Program to Iterate Using nditer()
Problem Statement
Write a Python program to iterate through a NumPy array using nditer().
Python Solution
import numpy as np
numbers = np.array([
[10, 20],
[30, 40]
])
for value in np.nditer(numbers):
print(value)
Sample Output
10
20
30
40
Explanation
np.nditer() is an efficient iterator that accesses every element regardless of the number of dimensions.
Concepts Covered
- nditer()
- Efficient Iteration
5. Python Program to Print Elements with Their Index
Problem Statement
Write a Python program to print each array element along with its index.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30])
for index, value in enumerate(numbers):
print(index, value)
Sample Output
0 10
1 20
2 30
Explanation
The enumerate() function returns both the index and the corresponding value.
Concepts Covered
- enumerate()
- Array Index
6. Python Program to Calculate the Sum Using Iteration
Problem Statement
Write a Python program to calculate the sum of all elements in a NumPy array using iteration.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40])
total = 0
for num in numbers:
total += num
print(total)
Sample Output
100
Explanation
Each element is added to the total variable during iteration.
Concepts Covered
- Sum of Array
- Loop
7. Python Program to Count Even Numbers Using Iteration
Problem Statement
Write a Python program to count the number of even elements in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 15, 20, 25, 30])
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
print(count)
Sample Output
3
Explanation
The program checks whether each element is divisible by 2.
Concepts Covered
- Conditional Statements
- Counting Elements
8. Python Program to Print Only Positive Numbers
Problem Statement
Write a Python program to print only the positive numbers from a NumPy array.
Python Solution
import numpy as np
numbers = np.array([-5, 10, -20, 30, 40])
for num in numbers:
if num > 0:
print(num)
Sample Output
10
30
40
Explanation
The if statement filters only positive values.
Concepts Covered
- Conditional Filtering
- Positive Numbers
9. Python Program to Find the Largest Element Using Iteration
Problem Statement
Write a Python program to find the largest element in a NumPy array using a loop.
Python Solution
import numpy as np
numbers = np.array([12, 45, 8, 90, 32])
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
print(largest)
Sample Output
90
Explanation
The program compares each element and stores the largest value.
Concepts Covered
- Maximum Value
- Comparison
10. Python Program to Find the Smallest Element Using Iteration
Problem Statement
Write a Python program to find the smallest element in a NumPy array using a loop.
Python Solution
import numpy as np
numbers = np.array([12, 45, 8, 90, 32])
smallest = numbers[0]
for num in numbers:
if num < smallest:
smallest = num
print(smallest)
Sample Output
8
Explanation
The program compares each element and stores the smallest value.
Concepts Covered
- Minimum Value
- Array Traversal
11. Python Program to Iterate Through a One-Dimensional NumPy Array
Problem Statement
Write a Python program to create a NumPy array containing numbers from 10 to 50 and iterate through each element to display them one by one.
Python Solution
import numpy as np
# Create NumPy array
array = np.arange(10, 51, 10)
print("Array Elements:")
for value in array:
print(value)
Sample Output
Array Elements:
10
20
30
40
50
Explanation
A one-dimensional NumPy array can be iterated using a simple for loop. Each iteration returns one element from the array.
Concepts Covered
- NumPy Array
- Array Iteration
- for Loop
- 1D Array
12. Python Program to Iterate Through a 2D NumPy Array Row by Row
Problem Statement
Write a Python program to create a 3×3 NumPy array and iterate through each row to display all elements.
Python Solution
import numpy as np
array = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
print("Array:")
print(array)
print("\nIterating Row Wise:")
for row in array:
print(row)
Sample Output
Array:
[[10 20 30]
[40 50 60]
[70 80 90]]
Iterating Row Wise:
[10 20 30]
[40 50 60]
[70 80 90]
Explanation
When iterating over a two-dimensional NumPy array, the loop returns one complete row at a time.
Concepts Covered
- 2D Array Iteration
- Rows
- Nested Arrays
- for Loop
13. Python Program to Iterate Through Every Element of a Multi-Dimensional NumPy Array
Problem Statement
Write a Python program to iterate through every individual element of a 3×3 NumPy array using the nditer() function.
Python Solution
import numpy as np
array = np.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print("Original Array:")
print(array)
print("\nIterating Every Element:")
for element in np.nditer(array):
print(element)
Sample Output
Original Array:
[[ 5 10 15]
[20 25 30]
[35 40 45]]
Iterating Every Element:
5
10
15
20
25
30
35
40
45
Explanation
np.nditer() is a powerful NumPy iterator that allows accessing each element individually from multi-dimensional arrays without using nested loops.
Concepts Covered
- nditer()
- Multi-dimensional Iteration
- Array Traversing
- NumPy Iterator
14. Python Program to Iterate Through NumPy Array and Calculate the Sum of Elements
Problem Statement
Write a Python program to iterate through a NumPy array and calculate the total sum of all elements without using the built-in sum() function.
Python Solution
import numpy as np
array = np.array([15, 25, 35, 45, 55])
total = 0
for value in array:
total = total + value
print("Array Elements:")
print(array)
print("\nTotal Sum:", total)
Sample Output
Array Elements:
[15 25 35 45 55]
Total Sum:
175
Explanation
The program manually adds each array element during iteration. This helps understand how aggregation operations work internally.
Concepts Covered
- Array Iteration
- Accumulator Logic
- Mathematical Operations
- 1D Array
15. Python Program to Iterate Through Columns of a NumPy Matrix
Problem Statement
Write a Python program to iterate through each column of a NumPy matrix and display column values separately.
Python Solution
import numpy as np
matrix = np.array([
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
])
print("Original Matrix:")
print(matrix)
print("\nColumn Wise Iteration:")
for column in matrix.T:
print(column)
Sample Output
Original Matrix:
[[10 20 30]
[40 50 60]
[70 80 90]]
Column Wise Iteration:
[10 40 70]
[20 50 80]
[30 60 90]
Explanation
By using .T (transpose), rows are converted into columns. This allows iteration through columns easily.
Concepts Covered
- Matrix Transpose
- Column Iteration
- 2D Arrays
- NumPy Operations
Chapter Summary
In this chapter, you learned how to iterate through one-dimensional, two-dimensional, and three-dimensional NumPy arrays. You also practiced using nditer(), enumerate(), and loops to perform common operations such as finding the sum, counting even numbers, filtering positive values, and identifying the largest and smallest elements.
Key Takeaways
- A
forloop is the simplest way to iterate through a NumPy array. - Nested loops are required for multi-dimensional arrays.
np.nditer()provides an efficient way to traverse arrays of any dimension.enumerate()returns both the index and value of each element.- Iteration can be used to calculate sums, counts, maximum values, and minimum values.
- Conditional statements help filter array elements during iteration.
- Array iteration is a fundamental skill for data analysis and machine learning.
Frequently Asked Questions (FAQs)
1. What is array iteration in NumPy?
Array iteration is the process of accessing each element of a NumPy array one by one using loops or built-in iterators.
2. Which loop is commonly used to iterate through a NumPy array?
The for loop is the most commonly used loop for iterating through NumPy arrays.
3. What is np.nditer() in NumPy?
np.nditer() is a built-in iterator that efficiently traverses NumPy arrays regardless of their dimensions.
4. When should I use nested loops?
Nested loops are used when working with two-dimensional or three-dimensional NumPy arrays.
5. Can I get both the index and value during iteration?
Yes. You can use the enumerate() function to get both the index and the value of each element.
6. Why is array iteration important?
Array iteration helps process, analyze, and manipulate data efficiently, making it essential for data science, machine learning, and scientific computing.
7. Is nditer() faster than nested loops?
In many cases, np.nditer() is more efficient and easier to use for traversing multi-dimensional arrays.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

