How to Loop over a Tensor in TensorFlow with for
A Python for loop works on a TensorFlow tensor the way it works on a list of lists: it hands you one row at a time. Each row is still a tensor, and so is each value in it. This guide loops over a small table of scores in TensorFlow 2.22.0, row by row and value by value.
Syntax
for <item> in <tensor>:
<code that uses item>
| Part | What it means |
|---|---|
<item> | A name for the current piece. Each time round the loop, it holds the next entry along the tensor's first axis, as a tensor with one axis fewer |
<tensor> | A tensor with at least one axis. For a table, each piece is a row |
<code that uses item> | The indented lines that run once for each piece |
The tf.Tensor API page lists __iter__(), the method Python's for calls, without a description. TensorFlow's own source code, in tensor.py, describes the class that does the looping: it "Iterates over the leading dim of a Tensor", meaning the first axis.
The Example
Two players' scores in a quiz of three rounds: player 1 scored 7, 9 and 8, and player 2 scored 6, 10 and 9. The table has one row per player. The script loops over the rows, then over every score.
Prerequisites
- A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import tensorflow as tf
scores = tf.constant([[7, 9, 8], [6, 10, 9]])
for row in scores:
print(row)
for row in scores:
for score in row:
print(score.numpy(), end=" ")
print()

scores has the shape (2, 3): 2 rows of 3 scores. The first loop prints each row as it comes. The second loops over each row's scores too: score.numpy() turns a score into a plain number, end=" " puts a space after it instead of starting a new line, and the empty print() ends the line after each row.
Step 2: Run It
uv run python loop_demo.py
tf.Tensor([7 9 8], shape=(3,), dtype=int32)
tf.Tensor([ 6 10 9], shape=(3,), dtype=int32)
7 9 8
6 10 9

The first loop ran twice, once per row, and each row came out as a tensor of shape (3,). The nested loop went one level deeper, to the single scores, and printed them as plain numbers, one row per line. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Looping over a scalar, such as tf.constant(42) | TypeError: Cannot iterate over a scalar tensor. A scalar has no axis to loop along | Use its value directly: total.numpy() returned 42. total.shape.rank is 0 for a scalar |
| Expecting plain numbers from the loop | Each score is a tensor: print(score) showed tf.Tensor(7, shape=(), dtype=int32) | Call .numpy() on it, as the script does |
FAQ
How do I get each row's position too?
Wrap the tensor in Python's enumerate: for i, row in enumerate(scores) gave 0 with [7 9 8] and 1 with [ 6 10 9].
How do I loop over the columns instead?
Loop over the transposed tensor, which turns columns into rows: for column in tf.transpose(scores) gave each round's scores, [7 6], [ 9 10] and [8 9].
Do I need a loop to add up each row?
No. TensorFlow's own functions work on the whole tensor at once: tf.reduce_sum(scores, axis=1) returned each player's total, [24 25], with no loop.
Conclusion
A for loop over the (2, 3) tensor of scores gave its 2 rows, each a tensor of shape (3,), and a loop inside it gave the single scores, turned into plain numbers with .numpy(). A scalar has nothing to loop over, and sums and averages don't need a loop at all.
Additional Resources
- tf.Tensor — the API page.
- tensor.py at v2.22.0 — the source, with the iterator.
- tf.transpose — swapping rows and columns.
