How to Index and Slice a Tensor in TensorFlow
Getting one pixel out of a photo, one column out of a table or the first half of a batch all use the same tool: square brackets after the tensor. TensorFlow follows Python's rules for lists, with one index per axis. This guide picks five different pieces out of a small table in TensorFlow 2.22.0.
Syntax
<tensor>[<index>]
<tensor>[<index on axis 0>, <index on axis 1>]
<tensor>[<start>:<stop>]
| Part | What it means |
|---|---|
<index> | One position, counted from 0. A negative number counts back from the end: -1 is the last |
<index on axis 0>, <index on axis 1> | One index per axis, separated by commas: for a table, the row and then the column |
<start>:<stop> | A slice: from <start> up to but not including <stop>. Leave out either end to go to the edge, so : alone means everything |
The tensor guide sums up the rules: "TensorFlow follows standard Python indexing rules, similar to indexing a list or a string in Python, and the basic rules for NumPy indexing." It also says what each kind does to the shape: "Indexing with a scalar removes the axis" and "Indexing with a : slice keeps the axis".
The Example
A table of the numbers 1 to 12 in 3 rows of 4. The script picks out a row, a single value, a column, a 2 × 2 block and the last row.
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
table = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(table[0])
print(table[1, 2])
print(table[:, 0])
print(table[:2, 1:3])
print(table[-1])
![The slicing.py script in a macOS-style code window: a 3 by 4 table of the numbers 1 to 12, then prints of table[0], table[1, 2], table[:, 0], table[:2, 1:3] and table[-1].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/tensorflow/2026/tensorflow-slicing-01-code.webp)
| Index | Reads as |
|---|---|
table[0] | Row 0, the first row |
table[1, 2] | Row 1, column 2 |
table[:, 0] | Every row, column 0: the first column |
table[:2, 1:3] | Rows 0 and 1, columns 1 and 2 |
table[-1] | The last row |
Step 2: Run It
uv run python slicing.py
tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
tf.Tensor(7, shape=(), dtype=int32)
tf.Tensor([1 5 9], shape=(3,), dtype=int32)
tf.Tensor(
[[2 3]
[6 7]], shape=(2, 2), dtype=int32)
tf.Tensor([ 9 10 11 12], shape=(4,), dtype=int32)

Look at the shapes. table[0] used one plain index, so the row axis went away and a row of 4 was left: (4,). table[1, 2] used two, so both axes went and one number was left: 7, with the shape (). table[:2, 1:3] used only slices, so both axes stayed, shorter: (2, 2). 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 |
|---|---|---|
An index past the end: table[3] on a table with 3 rows | InvalidArgumentError: … slice index 3 of dimension 0 out of bounds. [Op:StridedSlice]. Rows are counted from 0, so the last one is 2 | Use table[2], or table[-1] for the last row |
Changing a value through an index: table[0, 0] = 100 | Tensors can't be changed in place, and Python stops with a TypeError | Build a new tensor, as Fixed: TypeError: 'EagerTensor' object does not support item assignment shows |
FAQ
How do I get every other row?
Add a step to the slice, as in a Python list's start:stop:step: table[::2] gave rows 0 and 2, [[1, 2, 3, 4], [9, 10, 11, 12]].
Can I index with a tensor?
With a scalar tensor, yes: the tf.Tensor API page shows foo[tf.constant(0), tf.constant(2)]. A tensor of several indexes isn't allowed: table[tf.constant([0, 2])] stopped with TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices. tf.gather(table, [0, 2]) picks rows by a list of indexes, and returned the same two rows as above.
Conclusion
Square brackets took a row, a value, a column, a block and the last row out of a 3 × 4 table. A plain index removes its axis and a slice keeps it, which is why the results had the shapes (4,), (), (3,), (2, 2) and (4,).
Additional Resources
- Indexing — single-axis and multi-axis indexing in TensorFlow's tensor guide.
tf.Tensor.__getitem__— what square brackets support.
