How do I split individual strings in a list?
data = ('Esperanza Ice Cream', 'Gregory Johnson', 'Brandies bar and grill') Return:
print(data) ('Esperanza', 'Ice', 'Cream', 'Gregory', 'Johnson', 'Brandies', 'bar', 'and', 'grill') How do I split individual strings in a list?
data = ('Esperanza Ice Cream', 'Gregory Johnson', 'Brandies bar and grill') Return:
print(data) ('Esperanza', 'Ice', 'Cream', 'Gregory', 'Johnson', 'Brandies', 'bar', 'and', 'grill') One approach, using join and split:
items = ' '.join(data) terms = items.split(' ') print(terms) ['Esperanza', 'Ice', 'Cream', 'Gregory', 'Johnson', 'Brandies', 'bar', 'and', 'grill'] The idea here is to generate a single string containing all space-separated terms. Then, all we need is a single call to the non regex version of split to get the output.
You can use itertools.chain for that like:
it.chain.from_iterable(i.split() for i in data) import itertools as it data = ('Esperanza Ice Cream', 'Gregory Johnson', 'Brandies bar and grill') print(list(it.chain.from_iterable(i.split() for i in data))) ['Esperanza', 'Ice', 'Cream', 'Gregory', 'Johnson', 'Brandies', 'bar', 'and', 'grill']