# Jit for tolist()

**URL:** <https://discuss.pytorch.org/t/jit-for-tolist/60926>\
**Category:** jit\
**Created:** [November 14, 2019, 3:37am UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926 "2019-11-14T03:37:26Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![driazati](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/driazati/32/15393_2.png) [@driazati](https://discuss.pytorch.org/u/driazati)\
**Post date:** [November 14, 2019, 9:59pm UTC](https://discuss.pytorch.org/t/jit-for-tolist/60926/2 "2019-11-14T21:59:29Z")

</div>

This is a known bug [https://github.com/pytorch/pytorch/issues/26752](https://github.com/pytorch/pytorch/issues/26752)

You can workaround by implementing it yourself, but it can be cumbersome due to the reasons explained in the bug report (plus the fact that TorchScript does not support generic functions or recursion):

```python
def my_1d_tolist(x):
    result: List[float] = []
    for i in x:
        result.append(i.item())
    return result

@torch.jit.script
def my_2d_tolist(x):
    result: List[List[float]] = []
    for i in x:
        result.append(my_1d_tolist(i))
    return result

x = torch.ones(2, 2)

# These calls should be the same
print(x.tolist())
print(my_2d_tolist(x))

```

---

_[View the full topic](https://discuss.pytorch.org/t/jit-for-tolist/60926)._
