Skip to content

Instantly share code, notes, and snippets.

@Willy-JL
Last active March 26, 2021 07:52
Show Gist options
  • Save Willy-JL/89edabcbd0a7df09a8a4666bfb79db0c to your computer and use it in GitHub Desktop.
Save Willy-JL/89edabcbd0a7df09a8a4666bfb79db0c to your computer and use it in GitHub Desktop.
Join words in a speech-like manner
example = ["abc", "def", "ghi"]
def join_words(words):
return (", ".join(words[:-1]) + (" and " if len(words) > 1 else "") + words[-1]) if len(words) > 0 else ""
result = join_words(example)
print(result)
# abc, def and ghi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment