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

