|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Convert string to list
Is there an easy way to convert a string that contains a list to a list? For example, '[1,2,3]' would convert to [1,2,3].
/Fredrik |
|
#2
|
||||
|
||||
|
split and eval
Ok, if I may ask why would you want to put a list within a string? Anyway you could use split to break the string apart..
>>> string = '[1,2,3]' >>> string = string[1:-1].split(',') >>> string ['1', '2', '3'] >>> however, the entries within the list will be converted to strings. If your looking to so somthing more advanced than this you could pass the string to eval, the return value should be the list contained within the string (My prefered way to do it). >>> string = '[1,2,3]' >>> list = eval(string) >>> list [1, 2, 3] >>> Hope this helps. Have fun, Mark. Last edited by netytan : July 23rd, 2003 at 11:09 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Convert string to list |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|