The result you wants is a bit blurry, but here a few ways
Using for loop to iterate over the string and combining pairs of words together:
s = 'john had a little blue car' words = s.split() for i in range(0, len(words), 2): w1 = words[i] w2 = words[i+1] print(w1, w2)
if you want to save the result than :
s = 'john had a little blue car' split_list = [] words = s.split() for i in range(0, len(words), 2): w1 = words[i] w2 = words[i+1] split_list.append([w1, w2])
after the code you will have in split_list in each cell the 2 of the words
Using the zip function to iterate over the string and combining pairs of words together (Might not familiar with Zip but its a very helpful tool):
s = 'john had a little blue car' words = s.split() for w1, w2 in zip(words[::2], words[1::2]): print(w1, w2)
if you want to save the result than :
s = 'john had a little blue car' split_list = [] words = s.split() for w1, w2 in zip(words[::2], words[1::2]): split_list.append([w1, w2])
after the code you will have in split_list in each cell the 2 of the words