In Python, arrays are used to store multiple values of the same data type. One of the most important operations when working with arrays is looping through elements.
Looping allows you to access each item in the array one by one, making it useful for processing data, calculations, and filtering values.
In this tutorial, you will learn different ways to loop through arrays in Python with clear examples.
Creating an Array
We use Python’s array module.
import array as arr
numbers = arr.array('i', [10, 20, 30, 40, 50])
print(numbers)1. Loop Through Array Using for Loop
The simplest way to loop through an array is using a for loop.
Example
for num in numbers:
print(num)Output
10
20
30
40
50Explanation
- Each element is accessed one by one
- No need to use index manually
2. Loop Using Index (range + len)
You can also loop using index positions.
Example
for i in range(len(numbers)):
print(numbers[i])Output
10
20
30
40
50Explanation
range(len(numbers))generates index values- Useful when you need index positions
3. Loop Using while Loop
Example
i = 0
while i < len(numbers):
print(numbers[i])
i += 1Output
10
20
30
40
504. Loop with Condition (Filter Values)
You can loop and filter values at the same time.
Example: Print values greater than 25
for num in numbers:
if num > 25:
print(num)Output
30
40
505. Loop and Modify Values
You can process each item during looping.
Example: Multiply each element by 2
for num in numbers:
print(num * 2)Output
20
40
60
80
1006. Loop Using Index for Modification
To modify an array, use index-based loop.
Example
for i in range(len(numbers)):
numbers[i] = numbers[i] + 5
print(numbers)Output
array('i', [15, 25, 35, 45, 55])7. Loop Using enumerate (Advanced)
Although arrays don’t directly use enumerate like lists, you can convert them.
Example
for index, value in enumerate(numbers):
print(index, value)Output
0 15
1 25
2 35
3 45
4 55Real-World Example: Student Marks
import array as arr
marks = arr.array('i', [85, 90, 78, 88])
total = 0
for mark in marks:
total += mark
print("Total Marks:", total)Output
Total Marks: 341Real-World Example: Product Prices
import array as arr
prices = arr.array('f', [99.99, 149.50, 200.75])
for price in prices:
print("Price:", price)Loop Types Summary
| Method | Description |
|---|---|
| for loop | Direct element access |
| range + len | Index-based looping |
| while loop | Manual control loop |
| condition loop | Filter values |
| modify loop | Change elements |
Common Mistakes
Mistake 1: Infinite While Loop
❌ Wrong:
i = 0
while i < len(numbers):
print(numbers[i])✔ Missing increment causes infinite loop
Mistake 2: Modifying Array While Iterating
for num in numbers:
numbers.remove(num)✔ This may skip elements or cause errors
Mistake 3: Wrong Index Access
print(numbers[10])✔ Causes IndexError
Safe Loop Example
for num in numbers[:]:
print(num)Array Loop Methods Summary
| Method | Use |
| for loop | Simple iteration |
| while loop | Controlled iteration |
| index loop | Position-based access |
| condition loop | Filtering |
| modify loop | Updating values |
Practice Exercise 1
Print all even numbers from an array.
Practice Exercise 2
Multiply each element by 3 and print results.
Conclusion
Looping arrays in Python is a fundamental skill for working with data.
You learned:
- Different ways to loop arrays
- How to use for, while, and index-based loops
- How to filter and modify array values
- Real-world examples
Mastering array loops is essential for data processing, analytics, and programming logic building.


0 Comments