Python: Iterating through an array

Articles may may have files attached at the end of the post

Iterating over an the elements of an array is pretty easy to do in python with the use of a for loop.

This tutorial will explain how to iterate through an array in python through a short script.

The script below will iterate through each elements of an array and print their value:

  1. #!/usr/bin/env python
  2. # iterating through an array
  3. arr = ("element1", "element2", "element3")
  4.  
  5. for i in arr:
  6.   print i

When unning this script, the output will be:

$ ./iterate.py
element1
element2
element3