The correct way to split on a character in python is to use the split
builtin. It will be much faster than anything you implement natively in python as it is a compiled C extension, as are all builtins at this point:
lst = "ballooolam".split("l")
However, as discussed in this question, this might not quite do what you expect. split
leaves empty strings in the list when there are tokens of zero length; I.E. if the delimeter is at the first/last position in the string, or if two delimeters are next to each other. This is so that doing word = 'l'.join(lst)
will return the original value; Without the empty strings, you would get 'balooolam'
back instead of 'ballooolam'
. If you want to remove these empty strings, you can do it easily with a list-comprehension:
def splitter(string, x):
return [token for token in string.split(x) if token]
The if token
will reject any string that is 'falsy', which empty strings are. If you'd also like to exclude whitespace-only strings from the final list, you can do that with a little tweak:
def splitter(string, x):
return [token for token in string.split(x) if token.strip()]
strip()
removes any leading/trailing whitespace from a string. In the case of a whitespace-only string, this will result in an empty string, which will then be falsy.