How to Turn a PyTorch Tensor into a Python List with tolist()
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
tensor.tolist()
| Part | What it means |
|---|---|
tensor | The 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 numpyadds 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
import torch
grid = torch.tensor([[1, 2, 3], [4, 5, 6]])
as_list = grid.tolist()
print(as_list)
print(type(as_list))

grid.tolist() makes the list, and type() shows what kind of object it is.
Step 2: Run It
uv run python to_list.py
[[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.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/2026/pytorch-tolist-02-output.webp)
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
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 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.
uv run python to_list_gpu.py
[[1, 2, 3], [4, 5, 6]]
![The terminal after uv run python to_list_gpu.py: [[1, 2, 3], [4, 5, 6]].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/2026/pytorch-tolist-04-gpu.webp)
The same list came back, with no .cpu() needed: tolist() copied the values from the GPU itself.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
list(tensor) | It printed [tensor(1), tensor(2), tensor(3)]: a list of one-value tensors, not numbers | tensor.tolist(), which printed [1, 2, 3] |
Expecting tolist() to change the tensor | It returns a new list; the tensor stays as it was | Keep 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
- Tensor.tolist — PyTorch's reference for the method.
- Tensor.item — one value as a Python number.
- Tensors tutorial — PyTorch's introduction to tensors.
