When I first started working with TensorFlow, one of the things that confused me was how to take a regular Python list and turn it into a tensor.
It sounds simple, right? But when you’re dealing with machine learning models, you often get data in lists or arrays, and you need to convert them into tensors before TensorFlow can process them.
In this tutorial, I’ll show you how I convert lists to tensors in TensorFlow. I’ll cover different methods, explain them in plain English, and share complete code examples that you can run on your system.
What is a Tensor in TensorFlow?
Before we jump into the code, let me quickly explain what a tensor is.
A tensor is just a fancy name for a multi-dimensional array. If you’re familiar with NumPy arrays, tensors are very similar. The main difference is that tensors are optimized for GPU/TPU acceleration and are the core data structure in TensorFlow.
So, whenever you’re working with TensorFlow, you’ll need to convert your data into tensors.
Method 1 – Use Python’s tf.convert_to_tensor()
The most common way I convert a list into a tensor is by using the tf.convert_to_tensor() function.
Here’s a simple example:
import tensorflow as tf
# A simple Python list
my_list = [10, 20, 30, 40, 50]
# Convert list to tensor
tensor = tf.convert_to_tensor(my_list, dtype=tf.int32)
print("Tensor:", tensor)
print("Tensor values:", tensor.numpy())
print("Tensor shape:", tensor.shape)
print("Tensor data type:", tensor.dtype)Output:
Tensor: tf.Tensor([10 20 30 40 50], shape=(5,), dtype=int32)
Tensor values: [10 20 30 40 50]
Tensor shape: (5,)
Tensor data type: <dtype: 'int32'>You can refer to the screenshot below to see the output

I like this method because it’s simple and works for most use cases. You can also specify the data type (dtype) to make sure TensorFlow interprets the data correctly.
Method 2 – Use tf.constant() Method in Python
Another way I often use is tf.constant(). This function creates a constant tensor directly from a list.
import tensorflow as tf
# A nested list (2D data)
my_list = [[1, 2, 3], [4, 5, 6]]
# Convert list to tensor
tensor = tf.constant(my_list, dtype=tf.float32)
print("Tensor:", tensor)
print("Tensor values:\n", tensor.numpy())
print("Tensor shape:", tensor.shape)Output:
Tensor: tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
Tensor values:
[[1. 2. 3.]
[4. 5. 6.]]
Tensor shape: (2, 3)You can refer to the screenshot below to see the output

I use this method when I know the data won’t change and I want to treat it as a constant throughout my program.
Method 3 – Convert Lists with Different Data Types
Sometimes, my lists contain mixed data types, like integers and floats. TensorFlow automatically converts them into a common type.
import tensorflow as tf
my_list = [1, 2.5, 3, 4.75]
tensor = tf.convert_to_tensor(my_list)
print("Tensor:", tensor)
print("Tensor values:", tensor.numpy())
print("Tensor dtype:", tensor.dtype)Output:
Tensor: tf.Tensor([1. 2.5 3. 4.75], shape=(4,), dtype=float32)
Tensor values: [1. 2.5 3. 4.75]
Tensor dtype: <dtype: 'float32'>You can refer to the screenshot below to see the output

This is really useful when dealing with real-world datasets where values are not always consistent.
Method 4 – Convert Large Lists (Real-World Example)
In real projects, I often deal with large lists, like daily sales data from a U.S.-based retail store. Let’s say we have sales numbers for 30 days in a month.
import tensorflow as tf
# Example: Daily sales for a month (in USD)
sales_list = [250, 300, 275, 400, 500, 450, 380, 420, 390, 410,
600, 620, 580, 590, 605, 615, 630, 640, 650, 670,
700, 720, 710, 730, 740, 750, 760, 770, 780, 800]
# Convert to tensor
sales_tensor = tf.convert_to_tensor(sales_list, dtype=tf.float32)
print("Sales Tensor shape:", sales_tensor.shape)
print("First 5 days sales:", sales_tensor.numpy()[:5])
print("Average daily sales:", tf.reduce_mean(sales_tensor).numpy())Output:
Sales Tensor shape: (30,)
First 5 days sales: [250. 300. 275. 400. 500.]
Average daily sales: 555.0You can refer to the screenshot below to see the output

With just a few lines of code, I can now use TensorFlow operations like tf.reduce_mean() to analyze the data.
Method 5 – Convert Lists with NumPy Before TensorFlow
Sometimes I prefer to convert lists into NumPy arrays first and then into tensors. This gives me more flexibility when preprocessing data.
import tensorflow as tf
import numpy as np
# Python list
my_list = [[10, 20], [30, 40], [50, 60]]
# Convert list to NumPy array
np_array = np.array(my_list)
# Convert NumPy array to Tensor
tensor = tf.convert_to_tensor(np_array, dtype=tf.int32)
print("Tensor:", tensor)
print("Tensor shape:", tensor.shape)This method is especially handy when I need to normalize or reshape the data before passing it into TensorFlow.
Common Mistakes to Avoid
- Forgetting the dtype – If you don’t specify the data type, TensorFlow may assign an unexpected type.
- Irregular lists – Lists with inconsistent lengths (ragged lists) will throw errors. You need to use tf.ragged.constant() in such cases.
- Confusing tf.constant and tf.convert_to_tensor – Both work, but tf.constant is best for fixed values, while tf.convert_to_tensor is more flexible.
Converting a Python list into a TensorFlow tensor is one of the first steps in preparing your data for machine learning.
I usually go with tf.convert_to_tensor() because it’s simple and flexible. But if I know my data won’t change, tf.constant() is a great choice.
By practicing these methods, you’ll quickly get comfortable moving your data into TensorFlow and focus more on building powerful models.
Other TensorFlow tutorials you may also like:
- Create a Filter() Function in Python Tkinter
- Call a Function Within a Function in Python
- Use Python Functions with Optional Arguments
- Use Lambda Functions in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.