# convert string to list

I have a string of the form, say, '[(1,2),(2,3)]' and want to obtain the list [(1,2),(2,3)] instead.

1

You can use the `eval`

Python function:

```
sage: s='[(1,2),(2,3)]' ; s
'[(1,2),(2,3)]'
sage: type(s)
<type 'str'>
sage: L = eval(s) ; L
[(1, 2), (2, 3)]
sage: type(L)
<type 'list'>
```

Note however that the string is not preparsed, so you will get Python ints, note Sage integers:

```
sage: type(L[0][0])
<type 'int'>
```

To "benefit" from Sage preparsing, you can do:

```
sage: L = eval(preparse(s)) ; L
[(1, 2), (2, 3)]
sage: type(L[0][0])
<type 'sage.rings.integer.Integer'>
```

0

**
answered 2017-01-04 01:10:25 +0200 **

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Thank you very much for your answer! It works if I use the string s. If instead I use my_string (which, on the surface, looks pretty much like s, but was obtained from opening a .sage file containing the string) then I get: TypeError: 'tuple' object is not callable !!

Please start posting anonymously - your entry will be published after you log in or create a new account.

Asked: ** 2017-01-03 23:09:51 +0200 **

Seen: **1,061 times**

Last updated: **Jan 04 '17**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.