You can use str.partitionfor this:
>>> from itertools import chain
>>> images = ['pdf-one', 'gif-two', 'jpg-three']
>>> list(chain.from_iterable([[a, b+c] for a, b, c
in (x.partition('-') for x in images)]))
['pdf', '-one', 'gif', '-two', 'jpg', '-three']
Using the generator function for a more readable solution:
def my_split(seq):
for item in seq:
a, b, c = item.partition('-')
yield a
yield b+c
>>> list(my_split(images))
['pdf', '-one', 'gif', '-two', 'jpg', '-three']
source
share