1

My question is probably simple but I don't be able to figure it out.

Consider this code where mylist can have any number of dimension:

mylist = [[1,2,3,4],[5,6,7]]
mylist
[[1,2,3,4],[5,6,7]]

It's easy to cconvert it to string:

myString = str(myList)
myString
>'[[1,2,3,4],[5,6,7]]'

But how to easily convert it back to the same list ? I never get it work in any situation using .join or .split. I want it work in any case of the list was configured.

thanks

Jean-Francois Gallant
  • 13,583
  • 6
  • 20
  • 24
  • 1
    `ast.literal_eval`, but why do you want to do this? – tzaman Oct 30 '15 at 15:11
  • I code for a 3D Software call Blender. The python API make possible to save data property with the saved file like float , int , string but not list with multiple dimension. I just figured to save my list has string and retrieve it in some way later. – Jean-Francois Gallant Oct 30 '15 at 23:10

1 Answers1

2

Now mylist is a string. (from your code[enter link description here][1]) So now we pass mylist to eval. Code Example below :

mylist=eval('[[2,3,4,5,6,2],[2,3,4,5,6,2]]')

Saiful Azad
  • 1,823
  • 3
  • 17
  • 27
  • 1
    While this answer is probably correct and useful, it is preferred if you [include some explanation along with it](http://meta.stackexchange.com/q/114762/159034) to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Oct 30 '15 at 21:36
  • @Saiful Ecellent! do you know how to do the same in an Ansible task ? – Ahmed Hussein Mar 11 '20 at 18:21
  • @AhmedHussein, I do ansible. However, I did not use this yet. – Saiful Azad Mar 12 '20 at 08:23