shaunaloh
shaunaloh

Reputation: 11

Converting string (with list of list) to list

I need to convert a string which has nested lists in it, into a list Is there a way to do this without the use of eval() and non standard python libraries? An example would be:

input = '[4,[5,6],7,[8,[9,10]],11,12,[13,14]]'
output = [4,[5,6],7,[8,[9,10]],11,12,[13,14]]

Upvotes: 1

Views: 58

Answers (1)

iGian
iGian

Reputation: 11183

To convert the string

without the use of eval() and non standard python libraries

you could make use of json, which is part of the Python Standard Library:

import json

input_as_string = '[4,[5,6],7,[8,[9,10]],11,12,[13,14]]'
input_as_list = json.loads(input_as_string)

print(input_as_list.__class__)
#=> <class 'list'>

Upvotes: 1

Related Questions