[Tutor] string to list

Jose Amoreira ljmamoreira at gmail.com
Wed Feb 10 13:12:58 CET 2010


On Wednesday 10 February 2010 11:26:25 am Owain Clarke wrote:
> Please excuse the obviousness of my question (if it is), but I have
> searched the documentation for how to generate a list e.g. [(1,2),
> (3,4)] from a string "[(1,2), (3,4)]". I wonder if someone could point
> me in the right direction.
> 
> Many thanks
> 
> Owain Clarke
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
Hi
One possibility is to use the eval function. Look at this snippet from an idle 
session:
>>> s="[(1,2),(3,4)]"
>>> lst=eval(s)
>>> lst
[(1, 2), (3, 4)]
>>> lst[0]
(1, 2)
>>> 
Hope this helped
Jose Amoreira


More information about the Tutor mailing list