You get prettier code with:

for iy, ix in np.ndindex(a.shape):
    print(a[iy, ix])

resulting in:

1
2
3
4
5
6
7
8
9
10
11
12
Answer from Markus Dutschke on Stack Overflow
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
import numpy as np arr = np.array([1, 2, 3]) for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']): print(x) Try it Yourself » · We can use filtering and followed by iteration.
Discussions

A quick question about iterating through 2d arrays with Python.
Sure. Create a new list, loop through the rows of the old list, and append the elements you want to the new list. Basically, do what the comprehension would’ve done, just with more code. More on reddit.com
🌐 r/learnprogramming
2
1
September 24, 2021
Is there a NumPy'-thonic'/efficient way to iterate over each elem in a 2D array?
This code will slide a window over the entire board and return a 3x3 array for each cell, I would suggest putting a border around the board so each cell is looked at: import numpy as np gameOfLifeBoard = [[0,0,0,0,0,0,0,0,0], [0,1,0,0,0,0,0,0,0], [0,0,1,1,0,0,0,0,0], [0,1,1,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0]] result = np.lib.stride_tricks.sliding_window_view(gameOfLifeBoard,(3,3)) print(result) The output: [[[[0 0 0] [0 1 0] [0 0 1]] [[0 0 0] [1 0 0] [0 1 1]] [[0 0 0] [0 0 0] [1 1 0]] [[0 0 0] [0 0 0] [1 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] [[[0 1 0] [0 0 1] [0 1 1]] [[1 0 0] [0 1 1] [1 1 0]] [[0 0 0] [1 1 0] [1 0 0]] [[0 0 0] [1 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] [[[0 0 1] [0 1 1] [0 0 0]] [[0 1 1] [1 1 0] [0 0 0]] [[1 1 0] [1 0 0] [0 0 0]] [[1 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] [[[0 1 1] [0 0 0] [0 0 0]] [[1 1 0] [0 0 0] [0 0 0]] [[1 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]] [[[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0]]]] You can then reshape the returned data in to rows which makes it easier to work with: result = result.reshape((result.shape[0]*result.shape[1], 9)) print(result) The output: [[0 0 0 0 1 0 0 0 1] [0 0 0 1 0 0 0 1 1] [0 0 0 0 0 0 1 1 0] [0 0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 1 0 0 0 1 0 1 1] [1 0 0 0 1 1 1 1 0] [0 0 0 1 1 0 1 0 0] [0 0 0 1 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 1 0 1 1 0 0 0] [0 1 1 1 1 0 0 0 0] [1 1 0 1 0 0 0 0 0] [1 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 1 1 0 0 0 0 0 0] [1 1 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0]] You are then able to easily sum and search the rows. Note that each row is a cell and it's neighbors and index 4 is the cell. # sum of neighbors for x in result: s = sum(x) - x[4] More on reddit.com
🌐 r/learnpython
15
3
March 25, 2022
Why can't I easily loop over numpy arrays
You can loop over numpy arrays: In [31]: a = np.array([1, 2, 3, 4, 5]) ...: for v in a: ...: print(v) ...: 1 2 3 4 5 It's just not recommended because it's slow. It's not slower than iterating through a normal Python list, but it's much slower than it would otherwise be vectorized (which is a big deal if you're dealing with thousands of values, which is pretty common for many numpy users). Also, np.where() returns two values for a 2D array, first the row indices (i.e., y), and then the column indices (i.e., x). Arrays are indexed by (row, column) which is (y, x), not (x, y) which is what you might be used to. This is true of most programming languages, FWIW and has nothing to do with numpy (a pure python 2d list would be indexed the same). I don't understand what your question is at the end; the above may or may not have shed light on it, but please expand a bit more if that didn't help. More on reddit.com
🌐 r/learnpython
6
3
December 12, 2018
Looping over first two indices in 4-Dimensional Array?
Just for reference, the correct way to write this as for loops would look like this: for i in range(window.shape[0]): for j in range(window.shape[1]): But this being NumPy, the above is not the best way to solve this. You almost never need to manually loop over NumPy arrays. I'm not quite following what you are doing in the loop body, since your condition appears to be essentially a == 1 and a-1 == 1 and a-1 == 1, so simultaneously redundant and contradictory. Also, P11 is a 3x3 array that you only ever modify one value of. But as an example, if you wanted to count how many of the middle values of the 3x3 arrays in window are equal to 1, you could write this vectorized like so: num_11_ones = (window[:, :, 1, 1] == 1).sum() This exploits the fact that True and False convert to 1 and 0 automatically for arithmetic operations, so summing up a boolean array gives you the number of Trues it contains. If there are more conditions than that, you can create one boolean array for each condition, combine them with the & (AND) or | (OR) operators as appropriate, and then sum them up at the end: num_11_ones_or_zeros = ((window[:, :, 1, 1] == 1) | (window[:, :, 1, 1] == 0)).sum() More on reddit.com
🌐 r/learnpython
2
2
November 3, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-iterating-over-array
Numpy - Iterating Over Arrays - GeeksforGeeks
July 23, 2025 - Python · import numpy as np # Create a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Iterate over rows for row in arr_2d: print(row) Output · [1 2 3] [4 5 6] [7 8 9] Explanation: arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) creates a 2D array with three rows and three columns. for row in arr_2d: loops through each row of the 2D array, treating it as a separate one-dimensional array.
🌐
NumPy
numpy.org › doc › stable › reference › arrays.nditer.html
Iterating over arrays — NumPy v2.5 Manual
The iterator object nditer, introduced in NumPy 1.6, provides many flexible ways to visit all the elements of one or more arrays in a systematic fashion. This page introduces some basic ways to use the object for computations on arrays in Python, then concludes with how one can accelerate the ...
🌐
Kaggle
kaggle.com › general › 521858
Numpy Arrays Iterating | Kaggle
# Create 2D array arr1_2d = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]) # Iterate through 2D array for y in arr1_2d : # Iterate on each scalar element of the 2D array for z in y : print(z) ... Iterating through a 3D NumPy array prints ...
🌐
Reddit
reddit.com › r/learnprogramming › a quick question about iterating through 2d arrays with python.
r/learnprogramming on Reddit: A quick question about iterating through 2d arrays with Python.
September 24, 2021 -

This is melting my brain for whatever reason...

so if I have a 2d list like:

list = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]

How would I just extract a certain index of every list?

I know I can do a list comprehension of

[i[0] for i in list]

beside using modules and imports is there any way to do this normally with a for loop?????

Thanks in advance

🌐
Medium
medium.com › @souvikdutta.aec › how-to-iterate-on-a-numpy-array-6c4cb1827846
How To Iterate on a Numpy Array | Medium
January 26, 2023 - Iterating in Numpy array means is going through the elements of the array elements one by one. If we want to iterate over a Multi Dimensional Array In Python then we can simply do this by using the for loop in Python.
Find elsewhere
🌐
YouTube
youtube.com › watch
Iterating Through Numpy Arrays - Numpy For Machine Learning 6 - YouTube
In this video we'll learn how to iterate thru Numpy Arrays using basic Python For Loops as well as the nditer() function that comes with Numpy.Iterating over...
Published: July 5, 2022
🌐
NumPy
numpy.org › doc › 2.2 › reference › arrays.nditer.html
Iterating over arrays — NumPy v2.2 Manual
The most basic task that can be done with the nditer is to visit every element of an array. Each element is provided one by one using the standard Python iterator interface.
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
Basic operations are simple with NumPy. If you want to find the sum of the elements in an array, you’d use sum(). This works for 1D arrays, 2D arrays, and arrays in higher dimensions.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-iterate-over-columns-in-numpy
Python - Iterate over Columns in NumPy - GeeksforGeeks
February 26, 2023 - Therefore, in order to perform such operations we need an array whose len(ary.shape) > 1 . To install NumPy on your python environment, type the following code in your OS's Command Processor (CMD, Bash etc): ... import numpy as np # Creating a sample numpy array (in 1D) ary = np.arange(1, 25, 1) # Converting the 1 Dimensional array to a 2D array # (to allow explicitly column and row operations) ary = ary.reshape(5, 5) # Displaying the Matrix (use print(ary) in IDE) print(ary) # This for loop will iterate over all columns of the array one at a time for col in range(ary.shape[1]): print(ary[:, col])
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-iterate-over-a-column-in-a-numpy-array-or-2D-matrix-in-python-
How to iterate over a column in a numpy array (or 2D matrix) in python ?
April 12, 2021 - How to iterate over a column in a numpy array (or 2D matrix) in python ? Note: in python column indices start at 0 (Zero-based numbering). To select an entire column, for instance column associated with index 4: data[:,4] returns here · array([6, 4, 8, 4, 9, 7, 0, 4, 6, 9]) Now to Iterate over a column: for e in data[:,4]: print(e) returns · 6 4 8 4 9 7 0 4 6 9 · Python – Iterate over Columns in NumPy · How to iterate through columns of a NumPy array in Python ·
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-iterate-over-a-row-in-a-numpy-array-or-2D-matrix-in-python-
How to iterate over a row in a numpy array (or 2D matrix) in python ?
April 12, 2021 - How to iterate over a row in a numpy array (or 2D matrix) in python ? Note: in python row indices start at 0 (Zero-based numbering). To select an entire row, for instance row associated with index 3: data[3,:] returns here · array([9, 8, 9, 1, 4, 2, 2, 3]) Now to Iterate over a row: for e in data[3,:]: print(e) returns ·
🌐
YouTube
youtube.com › watch
Python NumPy Tutorial for Beginners #8 - Iterating on Arrays - YouTube
Learn Python NumPy! In this eighth video of the NumPy tutorial series, we explore iterating on arrays!🔔NEW videos, tutorials and projects EVERY week so subs...
Published: February 17, 2021
🌐
GitHub
github.com › numpy › numpy › issues › 16985
Request for clarification - Iteration over python arrays is much faster compared to numpy arrays. · Issue #16985 · numpy/numpy
August 1, 2020 - Hi, I unexpectedly came across this fact that iterating over a python array.array() is much faster than iterating over a numpy array. Is there a specific reason why, given that the data on both arrays is held in contiguous memory? The te...
Author: numpy
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_iterating_over_array.htm
NumPy - Iterating Over Array
In this example, we iterate over an array using numpy.nditer() function, accessing each element in the default row-major order (C-style), and printing them sequentially − · import numpy as np # Create a 2D array arr = np.array([[1, 2, 3], ...
🌐
Ipython-books
ipython-books.github.io › 13-introducing-the-multidimensional-array-in-numpy-for-fast-array-computations
IPython Cookbook - 1.3. Introducing the multidimensional array in NumPy for fast array computations
A 2D array is a matrix; its shape is (number of rows, number of columns). The following figure illustrates the structure of a 3D (3, 4, 2) array that contains 24 elements: The slicing syntax in Python translates nicely to array indexing in NumPy.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python iterate over an array
Python Iterate Over an Array - Spark By {Examples}
May 31, 2024 - 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
🌐
You.com
codegrepper.com › code-examples › dart › numpy+2d+array+for+loop
You.com | Search and inference for AI
pip cannot find a binary wheel because the psycopg2 project hasn’t yet published wheels for Python 3.13.
🌐
Codegrepper
codegrepper.com › code-examples › python › how+to+iterate+over+rows+in+2d+numpy+array
how to iterate over rows in 2d numpy array Code Example
June 1, 2021 - x = [ ['0,0', '0,1'], ['1,0', '1,1'], ['2,0', '2,1'] ] for i in range(len(x)): for j in range(len(x[i])): print(x[i][j])