Skip to main content

How to Turn a PyTorch Tensor into a Python List with tolist()

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A tensor's values sometimes have to leave PyTorch: to be saved as JSON, printed in a report, or handed to code that expects plain Python numbers. tolist() turns a tensor into an ordinary Python list. This guide does that in PyTorch 2.15.0, for a tensor on the CPU and one on the GPU of an Apple M1 Pro.

Syntax​

Syntax
tensor.tolist()
PartWhat it means
tensorThe tensor to convert, on any device
.tolist()Returns its values as a Python list: a list of lists for a table, one level per dimension

PyTorch's reference says it "Returns the tensor as a (nested) list. For scalars, a standard Python number is returned, just like with item(). Tensors are automatically moved to the CPU first if necessary."

The Example​

A script turns a table of two rows and three columns into a Python list and prints the list and its type. A second script does the same with the table on the GPU.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

to_list.py
import torch

grid = torch.tensor([[1, 2, 3], [4, 5, 6]])
as_list = grid.tolist()

print(as_list)
print(type(as_list))

The to_list.py script in a macOS-style code window: a two-by-three tensor, grid.tolist() kept as as_list, and prints of the list and its type.

grid.tolist() makes the list, and type() shows what kind of object it is.

Step 2: Run It​

Terminal
uv run python to_list.py
Output
[[1, 2, 3], [4, 5, 6]]
<class 'list'>

The terminal after uv run python to_list.py: [[1, 2, 3], [4, 5, 6]] and class list.

The table became a list of two lists, one per row, printed without the tensor(...) around it, and its type is Python's own list. The command works unchanged in PowerShell.

Step 3: Do It from the GPU​

to_list_gpu.py
import torch

accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")

grid = torch.tensor([[1, 2, 3], [4, 5, 6]], device=device)
print(grid.tolist())

The to_list_gpu.py script in a macOS-style code window: the accelerator or the CPU picked as the device, the same table made on it, and a print of grid.tolist().

The first two lines pick the GPU when PyTorch can use one: How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS) explains them. device=device makes the table on it.

Terminal
uv run python to_list_gpu.py
Output
[[1, 2, 3], [4, 5, 6]]

The terminal after uv run python to_list_gpu.py: [[1, 2, 3], [4, 5, 6]].

The same list came back, with no .cpu() needed: tolist() copied the values from the GPU itself.

Common Mistakes​

MistakeWhat happensDo this instead
list(tensor)It printed [tensor(1), tensor(2), tensor(3)]: a list of one-value tensors, not numberstensor.tolist(), which printed [1, 2, 3]
Expecting tolist() to change the tensorIt returns a new list; the tensor stays as it wasKeep the result: as_list = grid.tolist()

FAQ​

What does tolist() give for a tensor with one value?​

A plain number, not a list: torch.tensor(5).tolist() printed 5, of type int.

How do I get one number out of a tensor?​

With item(), as How to Get a Python Number from a PyTorch Tensor with item() shows.

Conclusion​

grid.tolist() turned a two-by-three tensor into the Python list [[1, 2, 3], [4, 5, 6]], and did the same for a tensor on the M1 Pro's GPU without a .cpu() first.

Additional Resources​