a = ['Hi How are you', 'i am doing fine', 'how about you']
Here a is a list of sentences. I need something like this using python.
result = [Hi How are you i am doing fine how about you]
a = ['Hi How are you', 'i am doing fine', 'how about you']
Here a is a list of sentences. I need something like this using python.
result = [Hi How are you i am doing fine how about you]
You can use str.join(iterable)
- method takes input iterable like list, string, etc. and return a string which is the concatenation of the strings in iterable.
>>> [' '.join(a)]
['Hi How are you i am doing fine how about you']
The following code does this:
result = " ".join(a)
In case you want to split the sentences further like the title suggests:
a = ['Hi How are you', 'i am doing fine', 'how about you']
words = [w for x in a for w in x.split()]
print(words)
Gives:
['Hi', 'How', 'are', 'you', 'i', 'am', 'doing', 'fine', 'how', 'about', 'you']
Using a nested list-comprehension