On Thu, Apr 16, 2009 at 6:52 PM, johnf <jfabi...@yolo.com> wrote: > I am dealing with a database field that can only store strings. > I want to save the list to the field and when I retrieve the string convert it > back to a list. > > But this does NOT work. > mylist=[1,2,3,4] > mystr=str(mylist) > > newlist= list(mystr) > > I keep thinking there must be a simple way of get this done.
You can use eval() but that is generally discouraged as a security hole. If the list will just contain integers you can do something ad hoc: In [7]: mylist=[1,2,3,4] In [8]: mystr=str(mylist) In [10]: newlist = map(int, mystr[1:-1].split(',')) In [11]: newlist Out[11]: [1, 2, 3, 4] In Python 3 you can use ast.literal_eval(). >>> mylist = [1, 2, 3, 4] >>> mystr = str(mylist) >>> import ast >>> ast.literal_eval(mystr) [1, 2, 3, 4] You can use these recipes: http://code.activestate.com/recipes/511473/ http://code.activestate.com/recipes/364469/ Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor