Skip to content Skip to sidebar Skip to footer

How To Turn A Whole List Into A String And A String Back To A Whole List

hello i want to turn a list into string but the whole list a = [1,2,3] and i want it into b = '[1,2,3]' i also want it back from there so i get from b = '[1,2,3]' to c = [1,2,

Solution 1:

List to string is easy enough:

>>> str([1,2,3])
'[1,2,3]'

Or, as per John Gordon,

>>> json.dumps([1,2,3])
'[1,2,3]'

And you can convert back using json.loads:

>>>json.loads('[1,2,3]')
[1,2,3]

Post a Comment for "How To Turn A Whole List Into A String And A String Back To A Whole List"