# Convert Python List to PyTorch Tensor & Vice Versa (Examples)

Hi! This tutorial will show you examples of how to **turn a list to a PyTorch tensor and vice-versa** in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s get right into the Python code!

## Install and Import NumPy & torch

We will need to import both NumPy and torch. If you do not have either of the libraries installed in your Python environment, then you should run the lines of code below to install and import them; otherwise, you can skip to the next section in this tutorial.

# install numpy and torch pip install numpy pip install torch # import numpy and torch import numpy as np import torch

The above lines of code will install and import NumPy and torch in your Python environment.

## Create Sample List

Next, we are going to create the sample Python list that will be turned into a tensor in this tutorial. Tensors are the fundamental data structure in PyTorch, a popular deep-learning framework.

Therefore, in your Python IDE, run the code below to create the sample list.

my_list = [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>

## Example 1: List to Tensor | Turn List to Tensor Using tensor() Function

In this first example, we will use the tensor() function from the torch library to turn the list to a tensor.

my_tensor = torch.tensor(my_list) print(my_tensor) # tensor([1, 2, 3, 4, 5]) print(type(my_tensor)) # <class 'torch.Tensor'>

The `tensor()`

function creates a tensor with elements in the same data type as the Python list. In this case, since “my_list” contains integers, the resulting tensor will also contain integers.

## Example 2: List to Tensor | Turn List to Tensor Using as_tensor() Function

In this second example, we will use the as_tensor() function from the tensor library to transform the list to a tensor.

my_tensor = torch.as_tensor(my_list) print(my_tensor) # tensor([1, 2, 3, 4, 5]) print(type(my_tensor)) # <class 'torch.Tensor'>

In the above code, the `as_tensor()`

function creates a tensor that shares the same underlying data as “my_list”. This means that any changes made to the tensor will also be reflected in the original Python list, and vice versa.

## Example 3: List to Tensor | Turn List to Tensor Using from_numpy() Function

In this third example, we will use the from_numpy() function to convert the list to a tensor:

my_array = np.array(my_list) my_tensor = torch.from_numpy(my_array) print(my_tensor) # tensor([1, 2, 3, 4, 5]) print(type(my_tensor)) # <class 'torch.Tensor'>

In the above example, we used the array() function from the NumPy library to create a new NumPy array with the same data and shape as “my_list”. We then parsed “my_array” as an argument to the `from_numpy()`

function, which creates a new tensor with the same data and shape as “my_array”.

Now that we have demonstrated how to turn a Python list into a tensor, we will now consider examples of how to turn tensors back to lists.

## Example 1: Tensor to List | Turn Tensor to List Using tolist() Method

In this first example, we will use the tolist() method from the torch library to turn the tensor back to a list:

new_list = my_tensor.tolist() print(new_list) # [1, 2, 3, 4, 5] print(type(new_list)) # <class 'list'>

The `tolist()`

method converts the tensor to a Python list.

## Example 2: Tensor to List | Turn Tensor to List Using numpy().tolist() Function Chain

In this second example, we will use the `numpy().tolist()`

function chain to turn the tensor back to a list:

new_list = my_tensor.numpy().tolist() print(new_list) # [1, 2, 3, 4, 5] print(type(new_list)) # <class 'list'>

The code above first of all converts the tensors into a NumPy array, before they are then turned into a Python list.

## Example 3: Tensor to List | Turn Tensor to List Using For Loop

In this third example, we will use a for loop to transform the tensor object back to a list:

new_list = [] for t in my_tensor: new_list.append(t.item()) print(new_list) # [1, 2, 3, 4, 5] print(type(new_list)) # <class 'list'>

In the example above, we created an empty Python list called “new_list”. We then used a loop to iterate through each element in “my_tensor”, and used the `item()`

method to convert each element to a Python scalar, which we then appended to “new_list”.

In this tutorial, we have demonstrated how to turn a Python list into a tensor object and vice-versa. I hope you found it helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to turn a Python list into a tensor object and vice-versa? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to turn Python lists into tensors and vice-versa.

*The YouTube video will be added soon.*

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

- Access Dictionary within List in Python (Examples)
- Convert List to Bytes & Vice-Versa in Python (Examples)
- Convert Multiple Lists to Dictionary in Python (3 Examples)
- Convert List to Range in Python (2 Examples)
- Learn Python Programming

This post has shown how to **turn Python lists to tensors and vice-versa**. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.