Below is the list.
List=[['hello', 'how'], ['are', 'you', 'hope'], ['you,are,fine', 'thank', 'you']]
I want the output list as
List=[['hello', 'how'], ['are', 'you', 'hope'], ['you', 'are' ,'fine', 'thank', 'you']]
Try the following nested comprehension that recompiles the list in a single walk-through while splitting the tokens:
>>> [[token for el in sub for token in el.split(',')] for sub in List]
[['hello', 'how'], ['are', 'you', 'hope'], ['you', 'are', 'fine', 'thank', 'you']]
Using a simple iteration and str.split
Ex:
lst=[['hello', 'how'], ['are', 'you', 'hope'], ['you,are,fine', 'thank', 'you']]
result = []
for i in lst:
temp = []
for j in i:
temp += j.split(",")
result.append(temp)
print(result)
Output:
[['hello', 'how'],
['are', 'you', 'hope'],
['you', 'are', 'fine', 'thank', 'you']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With