I have this list [('[]',), ("['843997058062483456', '774956001223770122', '645597259458674707']",), ('[]',), ('[]',)]
and I want to turn it to [[], ['843997058062483456', '774956001223770122', '645597259458674707'], [], []]
Any idea?
CodePudding user response:
import json
original = [('[]',), ("['843997058062483456', '774956001223770122', '645597259458674707']",), ('[]',), ('[]',)]
def transform(lst):
return [json.loads(t[0].replace("'", '"')) for t in lst]
print(transform(original))
CodePudding user response:
You could use ast.literal_eval
in a nested list comprehension:
>>> from ast import literal_eval
>>> xs = [('[]',), ("['843997058062483456', '774956001223770122', '645597259458674707']",), ('[]',), ('[]',)]
>>> extracted = [literal_eval(s) for t in xs for s in t]
>>> extracted
[[], ['843997058062483456', '774956001223770122', '645597259458674707'], [], []]