How to generate "a, b, c, and d"?

Roy Smith roy at panix.com
Thu Dec 15 11:48:23 EST 2011


I've got a list, ['a', 'b', 'c', 'd'].  I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c').  Is there some standard way to do this, handling all the special cases?

[] ==> ''
['a'] ==> 'a'
['a', 'b'] ==> 'a and b'
['a', 'b', 'c', 'd'] ==> 'a, b, and c'

It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't.



More information about the Python-list mailing list