Skip to content

Instantly share code, notes, and snippets.

@Geekfish
Created August 20, 2018 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Geekfish/f6cb656ed82b049bf36f24f3113f43bf to your computer and use it in GitHub Desktop.
Save Geekfish/f6cb656ed82b049bf36f24f3113f43bf to your computer and use it in GitHub Desktop.
Comma Comma And
def comma_comma_and(_list, conj=u"and"):
"""
Accepts a list of strings and returns a single string, stitching it all
together.
Eg:
comma_comma_and(["Papa Bear", "Mama Bear", "Baby Bear"])
# > "Papa Bear, Mama Bear, and Baby Bear"
comma_comma_and(["Chocolate", "cake", "ice-cream"], conj="or")
# > "Chocolate, cake, or ice-cream"
comma_comma_and(["True", "false"], conj="or")
# > "True or false"
"""
_list = list(_list)
if not _list:
return u""
if len(_list) == 1:
return _list[0]
if len(_list) == 2:
# No Oxford comma
return (u" %s " % conj).join(_list)
return u", ".join(_list[:-1] + [(u"%s %s" % (conj, _list[-1]))])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment