How to index a list with a TensorFlow tensor?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A Python list and a TensorFlow tensor live in different execution models. If your index is a TensorFlow value, the safest approach is usually to convert the data to a tensor and use TensorFlow indexing operations such as tf.gather.
Why Direct Python List Indexing Is Fragile
A normal Python list expects a normal Python integer.
A TensorFlow tensor is not just an integer in another wrapper. It is a TensorFlow object that may be traced, staged into a graph, or computed later.
That means code like this is unreliable:
In eager mode, you may be tempted to convert with .numpy() and move on. That works only when you are already on the Python host side and do not care about graph execution, tracing, or model portability.
The Right Fix: Convert The Data And Use tf.gather
If the index is a tensor, keep the data in TensorFlow as well.
This works in eager mode and inside @tf.function, which is the real reason it is the preferred solution.
Single Index And Multiple Indices
tf.gather works for both one index and many indices.
That makes it a good general replacement for host-side list indexing.
Multi-Dimensional Cases
When the source is a matrix or higher-dimensional tensor, you can gather along a chosen axis.
If you need coordinate-style lookup such as "row 0, col 1" and "row 2, col 2", use tf.gather_nd instead.
What About .numpy()?
You can convert a scalar tensor to a Python integer in eager mode:
This is acceptable for small host-side scripts. It is the wrong pattern inside model code, inside @tf.function, or anywhere you want TensorFlow to own the whole computation.
A useful rule is:
- if the code is ordinary Python control flow, conversion may be okay
- if the code belongs to a TensorFlow pipeline, stay in TensorFlow ops
Boolean And Conditional Selection
If the goal is filtering rather than positional lookup, tf.boolean_mask may be clearer than gather.
This is often the better answer when the real problem is "pick elements meeting a condition."
Bounds And Dtypes Matter
TensorFlow indices should be integer tensors such as int32 or int64. Out-of-range indices will raise runtime errors.
If indices come from predictions or external input, validate them before gathering.
Common Pitfalls
The most common mistake is indexing a Python list with a tensor inside code that later moves into @tf.function. It may appear to work during experimentation and then fail when traced.
Another mistake is converting tensors to Python too early. That breaks TensorFlow's ability to optimize and stage the computation.
Developers also use tf.gather when they really need tf.gather_nd or tf.boolean_mask. Those operations solve different indexing problems.
Finally, check index dtype and range. Many indexing bugs are just invalid values, not TensorFlow syntax problems.
Summary
- A TensorFlow tensor is not a drop-in Python list index in graph-oriented code.
- Convert the source data to a tensor and use
tf.gatherfor normal indexed lookup. - Use
tf.gather_ndfor coordinate lookup andtf.boolean_maskfor filtering. - Converting with
.numpy()is only appropriate for simple eager-mode host logic. - Keep indexing inside TensorFlow ops when building model or pipeline code.

