Python Iterate Over an Array

How to use for loop to iterate over an array in Python? Iterate over an array is also referred to as looping through all the elements of an array which can easily perform by using for loops with syntax for x in arrayObj:.

Here, I will explain with examples of how to loop through every element of the NumPy array, loop through by getting index & value, and finally, iterate over a multi-dimensional array using for loop.

1. Quick Examples of Iterate Over an Array Using For Loop

Following are quick examples of iterating over an array in Python.


# Below are the quick examples

# Example 1: Iterate over an array using for loop
for x in arr:
  print(x)

# Example 2:  Iterate using nditer()
for x in np.nditer(arr):
  print(x)

# Example 3: Iterate getting indexx & value
for index, value in np.ndenumerate(arr):
  print(index,value)

# Example 4: Iterate 2-Dimensional array
for x in np.nditer(arr1):
  print(x)

# Example 5: Iterate 2-D array and get indexes & values
for index, value in np.ndenumerate(arr):
  print(index,value)

# Example 6: Iterate each dimensions of 2-D array 
for x in np.nditer(arr1, flags = ['external_loop'], order = 'F'):
  print(x)
 

2. Iterate Over Array Using for Loop

By using Python for loop with syntax for x in arrayObj: we can easily iterate or loop through every element in an array. In Python, you have to use the NumPy library to create an array. In order to use it, first you need to import the NumPy library.


import numpy as np
# Create a NumPy array
arr = np.array([20, 35, 40, 25, 50])

# Iterate over an array using for loop
for x in arr:
  print(x)

Yields below output.


# Output:
20
35
40
25
50

3. Iterate Over Array using nditer

We can also iterate over an array with the help of nditer function of NumPy. This function takes the array and argument. It will traverse each element in the given array and return them sequentially.


# Iterate over an array using for loop
for x in np.nditer(arr):
  print(x)
 

Yields the same output as above.

4. Iterate Index & Value of Array Using for Loop

We can also iterate both indexes and values of a given array using for loop and np.ndenumerate() function. For example,


# Iterate by getting index and value
for index, value in np.ndenumerate(arr):
  print(index, value)
 

Yields below output.


# Output:
(0,) 20
(1,) 35
(2,) 40
(3,) 25
(4,) 50

5. Iterate 2-Dimensional Array using For Loop

If you have a multi-dimensional array you need to perform multiple loops, to overcome this problem use nditer function of the NumPy module in Python. nditer is an efficient multidimensional iterator that provides various ways to iterate over each element of the array.

Let’s create a 2-Dimensional NumPy array using np. arange() and np.reshape() functions and then, iterate it using for loop with nditer() function. The below syntax will iterate both indexes and values of the given array and return them sequentially.


# Iterate 2-Dimensional array
# Create 2-D array
arr = np.arange(12)
arr1 = arr.reshape(3,4)

print("2-Dimensional array:\n", arr1)

# Iterate two dimensional array
print("Output:")
for x in np.nditer(arr1):
    print(x)

Yields below output.


# Output:
2-Dimensional array:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

#Output:
0
1
2
3
4
5
6
7
8

6. Iterate Indexes & Values of 2- Dimensional Array using For Loop

Let’s also see how to iterate over both indexes and values of a given 2-D array using Python for loop along with the np.ndenumerate() function. For example,


# Iterate 2-D array and get indexes & values
for index, value in np.ndenumerate(arr):
  print(index, value)

Yields below output.


# Output:
(0,) 0
(1,) 1
(2,) 2
(3,) 3
(4,) 4
(5,) 5
(6,) 6
(7,) 7
(8,) 8
(9,) 9
(10,) 10
(11,) 11

7. Iterate Single Dimension of 2-D Array using for Loop

Sometimes you may need to loop through single-dimension of 2-D NumPy array, We can use nditer() function with 'flags' param as 'external_loop‘ and 'order' param as 'F‘.


# Iterate each dimensions of 2-D array 
for x in np.nditer(arr1, flags = ['external_loop'], order = 'F'):
   print(x, end=' ')

Yields below output.


# Output:
[0 4 8] [1 5 9] [ 2  6 10] [ 3  7 11] 

7. Conclusion

In this article, I have explained several examples of how to iterate over for loop through every element of an array in Python. In order to use arrays in Python you have to use the NumPy library, and this library also provides methods nditer(), ndenumerate() to loop through single and multi-dimensional arrays. I have also explained how to iterate an array with index & value, and multi-dimensional arrays with examples.

Related Articles

References

Leave a Reply