TensorFlow
Data Conversion
Python
Machine Learning
Tensor Operations

TensorFlow Is there a way to convert a list with None type to a Tensor?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Not directly in the way most people expect. A regular dense TensorFlow tensor needs a consistent dtype and shape, so a Python list containing None cannot usually be converted as-is into a numeric tensor until you decide what None is supposed to mean.

Why None breaks tensor conversion

A dense tensor must contain values of one compatible type. This fails:

python
1import tensorflow as tf
2
3values = [1.0, None, 3.5]
4tensor = tf.convert_to_tensor(values)

TensorFlow cannot infer a clean numeric dtype because None is not a floating-point value, integer, or string tensor element in the normal sense.

So the real question is not "How do I force it?" The real question is "How should missing values be represented for my model or pipeline?"

Replace None with a numeric sentinel

If None means "missing numeric value," the simplest solution is to replace it with a sentinel such as 0.0, -1.0, or some domain-specific fallback:

python
1import tensorflow as tf
2
3values = [1.0, None, 3.5]
4cleaned = [0.0 if value is None else value for value in values]
5
6tensor = tf.convert_to_tensor(cleaned, dtype=tf.float32)
7print(tensor)

This is the most direct fix, but it works only if the sentinel value is meaningful or acceptable for the downstream model.

Keep a mask when missingness matters

Often, replacing None is not enough because the model should still know which entries were missing. In that case, create both a cleaned tensor and a mask:

python
1import tensorflow as tf
2
3values = [1.0, None, 3.5, None]
4
5cleaned = [0.0 if value is None else value for value in values]
6mask = [value is not None for value in values]
7
8value_tensor = tf.convert_to_tensor(cleaned, dtype=tf.float32)
9mask_tensor = tf.convert_to_tensor(mask, dtype=tf.bool)
10
11print(value_tensor)
12print(mask_tensor)

This is a much better pattern when missingness is informative and should not be silently erased by the preprocessing step.

Consider ragged or separate preprocessing for structural gaps

Sometimes None does not mean "missing scalar value." Sometimes it means "this position does not exist." That is a different problem.

For example, if you have variable-length sequences such as:

python
values = [[1, 2], None, [3]]

you may need to preprocess before tensor conversion or use a ragged representation for the actual sequences:

python
1import tensorflow as tf
2
3sequences = [[1, 2], [], [3]]
4ragged = tf.ragged.constant(sequences)
5print(ragged)

Here the empty list is a structural stand-in for "no sequence," which is much more tensor-friendly than leaving a Python None in the nested structure.

Convert in the input pipeline, not inside the model

The best place to normalize None is usually before the data becomes a tensor. For example:

python
1def normalize_missing(values):
2    cleaned = [0.0 if value is None else value for value in values]
3    mask = [value is not None for value in values]
4    return cleaned, mask
5
6
7values = [1.0, None, 3.5]
8cleaned, mask = normalize_missing(values)

Once the data is normalized, TensorFlow code stays much simpler. Feeding raw Python objects with None deep into the pipeline tends to create harder-to-debug dtype and shape problems later.

Do not confuse Python None with unknown tensor dimensions

TensorFlow also uses None in a different context, such as shape specifications:

python
inputs = tf.keras.Input(shape=(None, 16))

That None does not mean a missing value inside a tensor. It means the dimension is variable or unspecified at graph-build time. That is completely different from putting Python None in an actual data list.

Mixing up those two meanings causes a lot of confusion.

Common Pitfalls

The biggest mistake is trying to force tf.convert_to_tensor to accept None without first deciding on a representation for missing data.

Another common issue is replacing None with 0 and then forgetting that zero may be a real value in the data. If missingness matters, carry a mask too.

People also mix structural absence with missing scalar values. An empty sequence and a missing numeric measurement are not the same thing and should not always be encoded the same way.

Finally, do not confuse data-time None values with shape-time None placeholders in Keras input definitions.

Summary

  • A dense numeric TensorFlow tensor cannot usually contain Python None values directly.
  • Replace None with a real value before conversion if that matches your data semantics.
  • Add a mask tensor when missingness itself is important.
  • Use ragged or other structural preprocessing when None really means "no sequence" or "no element."
  • Treat missing data representation as a modeling decision, not just a conversion trick.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design