-2
def split(string,x):
    if string == "":
        return ""
    if string[0] == x :
        return split(string[1:],x)
    return string[0]+[split(string[1:],x)]

I want to give this func a string like "ballooolam" and "l" and I want this func to give me ["ba","ooo","am"] 3 days I'm thinking about it

Vincent Beltman
  • 2,064
  • 13
  • 27

1 Answers1

0

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.

Community
  • 1
  • 1
aruisdante
  • 8,875
  • 2
  • 30
  • 37