str.split finds all the occurrences of a separator and returns a list.
If you only need the first element then this is inefficient in two ways:
In [1]:
def get_first_split(input_string, split_by=' '):
try:
input_string_stripped = input_string.strip()
split_by_index = input_string_stripped.find(split_by)
if split_by_index == -1:
return input_string_stripped
else:
substring = input_string_stripped[:split_by_index].strip()
return substring
except AttributeError:
return ''
In [2]:
get_first_split(' this is _ a test', '_')
Out[2]:
In [3]:
get_first_split('some_group_1', '_')
Out[3]:
In [8]:
x = 'XQWRQW' * 100000
x += ' ' + ('XWQQWFW' * 100000)
In [9]:
len(x)
Out[9]:
In [46]:
%%timeit
x.split()[0]
In [12]:
%%timeit
x.split(maxsplit=1)[0]
In [47]:
%%timeit
get_first_split(x)