What is an elegant way to join a list of sentence parts so that the result is \"a, b, and c\" where the list is [ \'a\', \'b\', \'c\' ]? Specifying
list
[ \'a\', \'b\', \'c\' ]
Assuming len(words)>2, you can join the first n-1 words using ', ', and add the last word using standard string formatting:
len(words)>2
n-1
', '
def join_words(words): if len(words) > 2: return '%s, and %s' % ( ', '.join(words[:-1]), words[-1] ) else: return ' and '.join(words)