Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
  • Projects
  • IT Institute
  • Contact Us
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
  • Projects
  • IT Institute
  • Contact Us

Need Help? Talk to an Expert

+91 8000107255

Converting PyTorch Tensor to Python List

Home Converting PyTorch Tensor to Python List
Conversion of PyTorch Tensor to Python List
  • Written by krunallathiya21
  • March 24, 2025
  • 0 Com
PyTorch

Optimal .tolist() method

The most optimal and fastest way to convert PyTorch Tensor to Python List is to use the .tolist() method. It accepts a tensor, copies the data of that tensor, creates a list out of it, and returns it.

Converting PyTorch tensor to Python list using .tolist() method

If the input is a 1D tensor, it returns a flat list. If an input is a 2D tensor, it returns a list of lists.

import torch

# 1D Tensor
tensor = torch.tensor([11, 21, 19])

print(tensor) 
# Output: tensor([11, 21, 19])

print(type(tensor))
# Output: <class 'torch.Tensor'>

converted_list = tensor.tolist()

print(converted_list)
# Output: [11, 21, 19]

print(type(converted_list))
# Output: <class 'list'>

In this code, we took a 1D tensor and tried to convert it into a list, printing their data types using the type() method.

2D Tensor to nested list

Converting 2D PyTorch tensor to Nested Python list

If your input is a 2D tensor, as I mentioned earlier, it will return a list of lists (nested list).

import torch

# 2D Tensor
tensor = torch.tensor([[11, 18, 19], [21, 48, 60]])

print(tensor)
# Output: tensor([[11, 18, 19], 
#                 [21, 48, 60]])

print(type(tensor))
# Output: <class 'torch.Tensor'>

converted_list = tensor.tolist()

print(converted_list)
# Output: [[11, 18, 19], [21, 48, 60]]

print(type(converted_list))
# Output: <class 'list'>

GPU Tensor (CUDA)

Tensors mostly run the GPUs but Python lists can only run on CPUs. What if we want to convert GPU tensors to a list? Well, to do that, we need to first move the tensors from GPU to CPU using the .cpu() method. Then, we can perform the conversion using .tolist().

import torch

# GPU Tensor (CUDA)
tensor = torch.tensor([[11, 18, 19], [21, 48, 60]], device='cuda')

converted_list = tensor.cpu().tolist()

print(converted_list)
# Output: [[11, 18, 19], [21, 48, 60]]

If you forget to move to the CPU, you will encounter an error.

Tensor with Gradients

What if the tensor has requires_grad=True? Well, in that case, we need to use the .detach() method. So tensor.detach().cpu().tolist() is the right approach to avoid autograd tracking.

import torch


# Tensor with Gradients
grad_tensor = torch.tensor([11., 19., 21.], requires_grad=True)

converted_list = grad_tensor.detach().cpu().tolist()

print(converted_list)
# Output: [11.0, 19.0, 21.0]

Failing to detach gradients with .detach() retains autograd history.

0-dimensional tensor

If we work with A 0-dimensional tensor (scalar), the .tolist() method returns the Python scalar value. You can wrap it with “[]” to make it a list.

import torch

# Scalar Tensors
scalar_tensor = torch.tensor(11)

converted_list = [scalar_tensor.tolist()]

print(converted_list)
# Output: [11]

Sparse Tensors

What if your input tensor is sparse? Well, first, you need to convert it into a dense tensor using the .to_dense() method and then use the .tolist() method.

import torch

# Sparse Tensors
sparse_tensor = torch.sparse_coo_tensor(indices=[[0, 1], [1, 2]], 
                                        values=[3, 4], size=(2, 3))

print(sparse_tensor)
# Output: tensor(indices=tensor([[0, 1],
#                                [1, 2]]),
#                  values=tensor([3, 4]),
#                  size=(2, 3), nnz=2, layout=torch.sparse_coo)

converted_list = sparse_tensor.to_dense().tolist()

print(converted_list)
# Output: [[0, 3, 0], [0, 0, 4]]

Non-Contiguous Tensors

If your input tensor is non-contiguous, use the .contiguous() method before the list conversion.

import torch

# Non-Contiguous Tensors
non_contiguous_tensor = torch.tensor([[1, 2], [3, 4]]).t()  # Transposed (non-contiguous)

contiguous_tensor = non_contiguous_tensor.contiguous()

converted_list = contiguous_tensor.tolist()

print(converted_list)
# Output: [[1, 3], [2, 4]]

Using NumPy as an intermediate

The .numpy() method converts an input tensor into a numpy array and then chains the output with the .tolist() method to get the list. In this approach, we use a numpy array as an intermediate result, which you can use to perform some numeric operations and then convert to a list. This is not a direct approach.

import torch

# Using NumPy as an Intermediate
tensor = torch.tensor([[11, 18, 19], [21, 48, 60]])

converted_list = tensor.numpy().tolist()

print(converted_list)
# Output: [[11, 18, 19], [21, 48, 60]]

Only use the .numpy() approach if you want to execute numpy-related operations. Otherwise, use the .tolist() method directly on a tensor.

Using list comprehension

For fine-grained control, you can use list comprehension or for loop.

1D Tensor

import torch

tensor = torch.tensor([1, 2, 3])

main_list = [x.item() for x in tensor] 

print(main_list)
# Output: [1, 2, 3]

2D Tensor

import torch

# Using List Comprehension
tensor = torch.tensor([[11, 18, 19], [21, 48, 60]])

converted_list = [item.tolist() for item in tensor]

print(converted_list)
# Output: [[11, 18, 19], [21, 48, 60]]
If the input tensor is very large due to loop overhead, it is inefficient.
Post Views: 11
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • Golang
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About Us
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS
  • BLOG