-3

Here, 'list' is my list of strings, i want to split 'a b' into 'a','b' and merge it back into the list with other strings

list = ['abc','a b', 'a b c','1234']

Expected Output after splitting = ['abc','a','b','a','b','c','1234']

3 Answers3

2

Try this code

lis = ['abc', 'a b', 'a b c', '1234']
lis1 = []
for i in lis:
    b = i.split()
    lis1.extend(b)
print lis1

Output:-

['abc', 'a', 'b', 'a', 'b', 'c', '1234']
ImMJ
  • 206
  • 2
  • 5
2

try this code

' '.join(list).split(' ')

output

['abc', 'a', 'b', 'a', 'b', 'c', '1234']

1

Solved it by using :

[y for x in list for y in x.split(' ')]