Skip to content

Instantly share code, notes, and snippets.

@ajay2611
Created February 14, 2018 20:50
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajay2611/c3378bc78dda6af54a1cfd1960184140 to your computer and use it in GitHub Desktop.
Save ajay2611/c3378bc78dda6af54a1cfd1960184140 to your computer and use it in GitHub Desktop.
Python's split function implementation
def split(string, delimiter):
"""
Desc: Python's split function implementation
:param string: a string
:return: a list after breaking string on delimiter match
"""
result_list = []
if not delimiter:
raise ValueError("Empty Separator")
if not string:
return [string]
start = 0
for index, char in enumerate(string):
if char == delimiter:
result_list.append(string[start:index])
start = index + 1
if start == 0:
return [string]
result_list.append(string[start:index + 1])
return result_list
if __name__ == '__main__':
print(split("abc def xyz", " "))
print(split("abc", " "))
@enkinss
Copy link

enkinss commented Aug 25, 2023

won't work if delimiter is more than one character

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment