Python: split on either a space or a hyphen?
Question: In Python, how do I split on either a space or a hyphen?

Input:

    
    
    You think we did this un-thinkingly?
    

Desired output:

    
    
    ["You", "think", "we", "did", "this", "un", "thinkingly"]
    

I can get as far as

    
    
    mystr.split(' ')
    

But I don't know how to split on hyphens as well as spaces [and the Python
definition of split only seems to specify a
string](http://docs.python.org/2/library/stdtypes.html). Do I need to use a
regex?


Answer: 
    >>> import re
    >>> text = "You think we did this un-thinkingly?"
    >>> re.split(r'\s|-', text)
    ['You', 'think', 'we', 'did', 'this', 'un', 'thinkingly?']
    

As @larsmans noted, to split by multiple spaces/hyphens (emulating `.split()`
with no arguments) used `[...]` for readability:

    
    
    >>> re.split(r'[\s-]+', text)
    ['You', 'think', 'we', 'did', 'this', 'un', 'thinkingly?']
    

Without regex (regex is the most straightforward option in this case):

    
    
    >>> [y for x in text.split() for y in x.split('-')]
    ['You', 'think', 'we', 'did', 'this', 'un', 'thinkingly?']
    

Actually the answer by [@Elazar](http://stackoverflow.com/a/16926952/1219006)
without regex is quite straightforward as well (I would still vouch for regex
though)

