# Converting a Tensor to List in C++

**URL:** <https://discuss.pytorch.org/t/converting-a-tensor-to-list-in-c/94849>\
**Category:** C++\
**Created:** [September 1, 2020, 7:15pm UTC](https://discuss.pytorch.org/t/converting-a-tensor-to-list-in-c/94849 "2020-09-01T19:15:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhasin](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/b2d939/32.png) [@bhasin](https://discuss.pytorch.org/u/bhasin)\
**Post date:** [September 1, 2020, 7:15pm UTC](https://discuss.pytorch.org/t/converting-a-tensor-to-list-in-c/94849/1 "2020-09-01T19:15:13Z")

</div>

Hi,

How to convert a torch::Tensor to a list in C++? I am looking for a C++ equivalent of the following python code:

```auto
x
tensor([[0., 6.],
        [1., 2.]])
x.tolist()
[[0.0, 6.0], [1.0, 2.0]]

```

Thanks!

---

<div class="post-metadata">

**Author:** ![kaihsinwu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kaihsinwu/32/36742_2.png) [@kaihsinwu](https://discuss.pytorch.org/u/kaihsinwu)\
**Post date:** [September 4, 2020, 6:30pm UTC](https://discuss.pytorch.org/t/converting-a-tensor-to-list-in-c/94849/2 "2020-09-04T18:30:17Z")

</div>

The list in C++ is different than the list in python.

I would assume you probably want to convert tensor to some C++ standard container (like vector etc.)?

I don’t think it is possible now using libtorch’s API,

but using Tensor.data\_ptr\<float\>() will return the pointer float\*. Then you can start from here to convert the internal data to the C++ container, for example, naive traversal like [Convert tensor into an array](https://discuss.pytorch.org/t/convert-tensor-into-an-array/56721) or else.
