I have this list:
list1 = ["['word']", "['second', 'first']", "['first']"]
You can see it's not exactly a list of list of strings as the items were converted into strings.
I want to have this flattened output:
list2 = ['word', 'second', 'first', 'first']
Just a simple list of strings.
I 've tried using this way:
list2 = [ x.strip('[]') for x in list1]
But the issue is with the second item still considered as one item:
["'word'", "'second', 'first'", "'first'"]
So I tried splitting by comma:
list3 = [item.split(',') for x in list2 for item in x]
But it gave this output:
[["'"],
['w'],
['o'],
['r'],
['d'],
["'"],
["'"],
['s'],
['e'],
['c'],
['o'],
['n'],
['d'],
["'"],
['', ''],
[' '],
["'"],
['f'],
['i'],
['r'],
['s'],
['t'],
["'"],
["'"],
['f'],
['i'],
['r'],
['s'],
['t'],
["'"]]
Any idea how to fix this?
UPDATES - SOLUTION
With help from @AKX
list2 = [ast.literal_eval(item) for item in list1]
Then using this function:
def flatten(lst):
for el in lst:
if isinstance(el, list):
yield from el
else:
yield el
list3 = flatten(list2)
list(list3)
This gives:
['word', 'second', 'first', 'first']
UPDATE 2
An optimized and alternative solution from @waynelpu instead of flatten function, just use:
list2 = [inner for item in list1 for inner in ast.literal_eval(item)]