Python – Iterate over Array

Iterate over Array in Python

In Python, you can iterate over the items of given array using a For loop.

In this tutorial, you will learn how to iterate over the items of a given Python array, with examples.

To iterate over the items of a given array my_array in Python, use the For loop with the following syntax.

for item in my_array:
    print(item)

You have access to the respective item inside the loop during that iteration. In the following examples, we shall print the item to standard output. You may do required action on the item as per your requirement.

1. Iterate over Integer Array

In the following program, we take an integer array my_array, and then use a For loop to iterate over the items in the integer array.

Python Program

import array as arr

my_array = arr.array('i', [100, 200, 300, 400])

for item in my_array:
    print(item)
Run Code Copy

Output

100
200
300
400

2. Iterate over Character Array

In the following program, we take a character array my_array, and then use a For loop to iterate over the items in the character array.

Python Program

import array as arr

my_array = arr.array('u', 'apple')

for item in my_array:
    print(item)
Run Code Copy

Output

a
p
p
l
e

3. Iterate over Float Array

In the following program, we take a float array my_array, and then use a For loop to iterate over the floating point numbers in the array.

Python Program

import array as arr

my_array = arr.array('f', [3.14, 5.60, 8.96])

for item in my_array:
    print(item)
Run Code Copy

Output

3.140000104904175
5.599999904632568
8.960000038146973

Summary

In this Python Array tutorial, we learned how to iterate over the items of given array in Python, using For loop, with examples.

Related Tutorials

Code copied to clipboard successfully 👍