Q:

Fastest way to split a concatenated string into a tuple and ignore empty strings

I have a concatenated string like this:
my_str = 'str1;str2;str3;'

and I would like to apply split function to it and then convert the resulted list to a tuple, and get rid of any empty string resulted from the split (notice the last ';' in the end)
So far, I am doing this:
tuple(filter(None, my_str.split(';')))

Is there any more efficient (in terms of speed and space) way to do it?

A:

How about this?
    tuple(my_str.split(';')[:-1])
    ('str1', 'str2', 'str3')

You split the string at the ; character, and pass all off the substrings (except the last one, the empty string) to tuple to create the result tuple.

A:

That is a very reasonable way to do it. Some alternatives:

foo.strip(";").split(";") (if there won't be any empty slices inside the string)
[ x.strip() for x in foo.split(";") if x.strip() ] (to strip whitespace from each slice)

The "fastest" way to do this will depend on a lot of things… but you can easily experiment with ipython's %timeit:

In [1]: foo = "1;2;3;4;"

In [2]: %timeit foo.strip(";").split(";")
1000000 loops, best of 3: 1.03 us per loop

In [3]: %timeit filter(None, foo.split(';'))
1000000 loops, best of 3: 1.55 us per loop

A:

If you only expect an empty string at the end, you can do:
a = 'str1;str2;str3;'
tuple(a.split(';')[:-1])

or 
tuple(a[:-1].split(';'))

