I have created this function to parse the list:
listy = [\'item1\', \'item2\',\'item3\',\'item4\',\'item5\', \'item6\']
def coma(abc):
for i in abc[0:-
Correction for Craig’s answer above for a 2-element list (I’m not allowed to comment):
def oxford_comma_join(l):
if not l:
return ""
elif len(l) == 1:
return l[0]
elif len(l) == 2:
return l[0] + " and " + l[1]
else:
return ', '.join(l[:-1]) + ", and " + l[-1]
print(oxford_comma_join(['item1', 'item2', 'item3', 'item4', 'item5', 'item6']))
print(oxford_comma_join(['i1', 'i2']))
Results:
item1, item2, item3, item4, item5, and item6
i1 and i2