How to split list element into two?

list, python, sublist

Solution

In this case splitting with a regex makes for the most readable code:

import re

hyphensplit = re.compile('(-[a-z]+)').split
images = [part for img in images for part in hyphensplit(img) if part]

Demo:

>>> import re
>>> hyphensplit = re.compile('(-[a-z]+)').split
>>> images = ['pdf-one','gif-two','jpg-three']
>>> [part for img in images for part in hyphensplit(img) if part]
['pdf', '-one', 'gif', '-two', 'jpg', '-three']

Problem

If I have data stored in a list, e.g. ``` images = ['pdf-one','gif-two','jpg-three'] ``` How do I split these into multiple elements at the hyphen - not sublists. I.e. ``` images = ['pdf','-one','gif','-two','jpg','-three'] ``` not ``` images = [['pdf','-one'],['gif','-two'],['jpg','-three']] ```

Original source