Yandex

Course IndexCourse Index0

    ProgramGuru

    Array Iteration
    1D, 2D, and 3D NumPy Arrays


    Understanding how to iterate through arrays is fundamental when working with NumPy. Whether you're dealing with a simple 1D list of numbers or a complex 3D tensor, efficient iteration allows you to manipulate, analyze, and understand your data better.

    1D Array Iteration

    Let’s start with the most basic structure — a 1-dimensional array.

    import numpy as np
    
    arr_1d = np.array([10, 20, 30, 40])
    for element in arr_1d:
        print(element)
    10
    20
    30
    40

    Explanation

    This is straightforward. The for loop extracts each value in the 1D array one by one. It behaves just like a Python list during iteration.

    2D Array Iteration

    Now, let’s add a second dimension. A 2D array in NumPy is essentially a list of lists — or a matrix.

    arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
    for row in arr_2d:
        print("Row:", row)
    Row: [1 2 3]
    Row: [4 5 6]

    Explanation

    Each iteration in a 2D array gives you a 1D array — essentially, each row. To access individual elements, you can add a nested loop:

    for row in arr_2d:
        for item in row:
            print(item)
    1
    2
    3
    4
    5
    6

    3D Array Iteration

    3D arrays can be visualized as a stack of matrices. Each element in the first-level loop gives you a 2D array.

    arr_3d = np.array([
      [[1, 2], [3, 4]],
      [[5, 6], [7, 8]]
    ])
    
    for matrix in arr_3d:
        print("Matrix:
    ", matrix)
    Matrix:
     [[1 2]
     [3 4]]
    Matrix:
     [[5 6]
     [7 8]]

    Nested Iteration

    To reach each scalar element in a 3D array, use nested loops:

    for matrix in arr_3d:
        for row in matrix:
            for item in row:
                print(item)
    1
    2
    3
    4
    5
    6
    7
    8

    Using NumPy's nditer() for Universal Iteration

    Nested loops can get bulky. NumPy offers np.nditer() as a neat solution to iterate over any array, regardless of its dimensionality.

    for item in np.nditer(arr_3d):
        print(item)
    1
    2
    3
    4
    5
    6
    7
    8

    Why use nditer()?

    • It's dimension neutral
    • Cleaner code
    • Faster performance for large arrays

    Best Practices & Gotchas

    • Always check array shape before looping, especially for 3D or reshaped arrays.
    • Use nditer() when possible for clean, readable, and scalable code.
    • Avoid writing deeply nested loops if you can vectorize or flatten your operations.
    • Print intermediate values while debugging or learning — it builds intuition.

    Conclusion

    Iteration gives you control. Whether you're processing an image pixel-by-pixel or analyzing a dataset row-by-row, understanding how to loop through arrays is basic thing you need. With NumPy’s tools — from simple for loops to powerful utilities like nditer() — you can write clean, efficient, and intuitive code.



    Welcome to ProgramGuru

    Sign up to start your journey with us

    Support ProgramGuru.org

    You can support this website with a contribution of your choice.

    When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

    PayPal

    UPI

    PhonePe QR

    MALLIKARJUNA M