Python list to string spacing
Question: I have a list such as this

    
    
    list = ['Hi', ',', 'my', 'name', 'is', 'Bob', '!']
    

I wanted to convert this to a string, and originally, I found on stackoverflow
that .join() could be used. So i did:

    
    
    x = ' '.join(list)
    print(x)
    

which prints

    
    
    "Hi , my name is Bob !"
    

when what I want printed is:

    
    
    "Hi, my name is Bob!"
    

How do I not add spaces before periods and exclamation points? I want a more
general case so that I can for example read in a text file as a list, and
convert it to a string.

Thanks!


Answer: To solve it in a general case, use the `nltk`'s ["moses"
detokenizer](https://github.com/nltk/nltk/pull/1282):

    
    
    In [1]: l = ["Hi", ",", "my", "name", "is", "Bob", "!"]
    
    In [2]: from nltk.tokenize.moses import MosesDetokenizer
    
    In [3]: detokenizer = MosesDetokenizer()
    
    In [4]: detokenizer.detokenize(l, return_str=True)
    Out[4]: u'Hi, my name is Bob!'
    

The detokenizer is not yet a part of a stable `nltk` package. To be able to
use it now, install `nltk` directly from github.

