Python Program to Iterate Over an Array


An array is a data structure consisting of a set of elements (values) of the same data type, each element is identified by an index value. And the elements can be directly accessed by using their index numbers.

Arrays in Python

Python does not have a native array data structure. Instead, we can use the list data structure, NumPy, or array modules.

Here we will use list an array −

 [10, 4, 11, 76, 99]
  0   1   2   3   4      

The above array has 5 elements and each element is identified by an index value, starting from 0 to n-1, where n is the total number of elements.

Iterating over an array means accessing the array elements one by one. In the article below, we will see multiple ways to iterate over an array in Python.

Using For Loop

This is the easiest method to iterate an array in python programming. Here the for loop iterates all the array elements.

Example

In this example, The variable ele is the loop variable it stores the element corresponding to the each iteration. Rather than using the index number to accasses one by one element, here we can simply loop through and disply the array elements in the output.

lst = [1, 2, 3, 4, 5, 6] 
print ("The original array is: ",lst) 

# iterate over an array using the for loop
print("Iterate Over an Array: ")
for ele in lst:
   print(ele)

Output

The original array is:  [1, 2, 3, 4, 5, 6]
Iterate Over an Array: 
1
2
3
4
5
6

In the above example the array elements are iterated one by one using the for loop.

Example

In this example, we will iterate the array elements using the for loop and range() function together.

lst = [1, 2, 3, 4, 5, 6] 
print ("The original array is: ",lst) 

# iterate over an array using the for loop
print("Iterate Over an Array: ")
for i in range(len(lst)):
   print(lst[i])

Output

The original array is:  [1, 2, 3, 4, 5, 6]
Iterate Over an Array: 
1
2
3
4
5
6

Here, the variable “i” varies from 0 to length(array)-1, and it represents the index of the array elements.

Example

In this example, we will take an array of strings and we will iterate all string elements using the for loop.

lst = ['P', 'y', 't', 'h', 'o', 'n']
print ("The original array is: ",lst) 

# iterate over an array using the for loop
print("Iterate Over an Array: ")
for i in range(len(lst)):
   print(lst[i])

Output

The original array is:  ['P', 'y', 't', 'h', 'o', 'n']
Iterate Over an Array: 
P
y
t
h
o
n

Using While Loop

By using a while loop we can iterate the array elements. In python, while Loop generates the iterations repeatedly until a given condition is satisfied.

Example

In this example, the condition I < len(lest) is executed to iterate the all array elements.

lst = [1, 2, 3, 4, 5, 6] 
print ("The original array is: ",lst) 

# iterate over an array using the While loop
print("Iterate Over an Array: ")
i = 0

while i< len(lst):
   print(lst[i])
   i += 1

Output

The original array is:  [1, 2, 3, 4, 5, 6]
Iterate Over an Array: 
1
2
3
4
5
6

Note: The while loop will become an infinite loop if we forgot to increase the index value (i += 1).

Using Enumerate()

Enumerate() is a python built-in function, it takes an iterable-like array and returns a tuple containing a count and the values obtained from iterating over an iterable. Following is the syntax of this method –

enumerate(iterable, start=0)

Where,

  • Iterable  An iterable object (list, string, etc.,).

  • Start  A Number, default value is 0.

Example

In this example we will display the element along with the corresponding index value using the enumerate() function.

lst = [1, 2, 3, 4, 5, 6] 
print ("The original array is: ",lst) 

# iterate over an array using the for loop and enumerate() function
print("Iterate Over an Array: ")

for i, ele in enumerate(lst):
   print(i, ':', ele)

Output

The original array is:  [1, 2, 3, 4, 5, 6]
Iterate Over an Array: 
0 : 1
1 : 2
2 : 3
3 : 4
4 : 5
5 : 6

These are a few of the ways to iterate over an array in Python.

Updated on: 15-May-2023

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements