1

I have a list with sentences in Python:

list1 = ["This is the first sentence", "This is the second", "This is the third"]

I tried using split(" "):

sth = []
for i in list1:
    sth.append(i.split(" "))

But this way I get a 2D array, which contains regular lists from the sentences with their words, so something like this:

[["This", "is", "the", "first", "sentence"], ["This", "is", "the", "second"], ["This", "is", "the", "third"]]

I would like the outcome to be a regular list, this way:

["This", "is", "the", "first", "sentence", "This", "is", "the", "second", "This", "is", "the", "third"]

How can I achieve this?

Mishal
  • 450
  • 9
  • 27
jdsflk
  • 417
  • 9
  • 23

7 Answers7

4

try using extend instead of append. full code:

list1 = ["This is the first sentence", "This is the second", "This is the third"]
sth = []
for i in list1:
    sth.extend(i.split(" "))

Output:

['This', 'is', 'the', 'first', 'sentence', 'This', 'is', 'the', 'second', 'This', 'is', 'the', 'third']

snatchysquid
  • 1,283
  • 9
  • 24
4

This should do it:

' '.join(l).split()
Chris
  • 15,819
  • 3
  • 24
  • 37
1
sth = []
for i in list1:
    sth += i.split(" ")
Rufat
  • 536
  • 1
  • 8
  • 25
1

You can use chain

from itertools import chain
list(chain(*[["This", "is", "the", "first", "sentence"], ["This", "is", "the", "second"], ["This", "is", "the", "third"]]))
mad_
  • 8,121
  • 2
  • 25
  • 40
1

With list comprehension its a single line of code

list1 = [["This", "is", "the", "first", "sentence"], ["This", "is", "the", "second"], ["This", "is", "the", "third"]]

print( [item for subl in list1 for item in subl] )
OUT: ['This', 'is', 'the', 'first', 'sentence', 'This', 'is', 'the', 'second', 'This', 'is', 'the', 'third']
Nico Müller
  • 1,784
  • 1
  • 17
  • 37
0

You will have to use extend instead of append.

The basic difference between append and extend is that append is used to add one element at the end of the existing list while extend is used when we have to merge two list extend will iterate through the passed argument and add them to the existing list at a index of n+1,n+2,n+3…(where n is existing length of the list)

Here is the code:

list1 = ["This is the first sentence", "This is the second", "This is the third"]

sth = []
for i in list1:
    sth.extend(i.split(" "))   # Here is the change

Here is the output from terminal:

>>> print(sth)
['This', 'is', 'the', 'first', 'sentence', 'This', 'is', 'the', 'second', 'This', 'is', 'the', 'third']
Chris
  • 15,819
  • 3
  • 24
  • 37
Vandit Shah
  • 100
  • 1
  • 6
0

Try this:

list1 = ["This is the first sentence", "This is the second", "This is the third"]
l1 = []
for i in list1:
    l2 = i.split()
    for j in range(len(l2)):
        l1.append(l2[j])
print(l1)

output:

['This', 'is', 'the', 'first', 'sentence', 'This', 'is', 'the', 'second', 'This', 'is', 'the', 'third']
Dejene T.
  • 973
  • 8
  • 14
  • This is not the correct input set (his input consists of nested lists), it does not work with the correct input the OP provided. – Nico Müller Jun 01 '20 at 19:37