DEV Community

Discussion on: Daily Coding Puzzles - Nov 11th - Nov 16th

Collapse
 
aspittel profile image
Ali Spittel
def format_words(words):
    sentence = ''
    if words:
        words = [word for word in words if word]
        for index, word in enumerate(words):
            if index == 0:
                sentence += word
            elif index == len(words) - 1:
                sentence += " and " + word
            else:
                sentence += ", " + word
    return sentence