Created
August 20, 2018 14:09
-
-
Save Geekfish/f6cb656ed82b049bf36f24f3113f43bf to your computer and use it in GitHub Desktop.
Comma Comma And
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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