def humanize_list(list_, last_sep=' and ', sep=', ', wrap_item_with=''):
"""
Return a string with a natural enumeration of items
source: Ipython.utils.text (modified)
>>> humanize_list(['a', 'b', 'c', 'd'])
'a, b, c and d'
>>> humanize_list(['a', 'b', 'c'], ' or ')
'a, b or c'
>>> humanize_list(['a', 'b', 'c'], ', ')
'a, b, c'
>>> humanize_list(['a', 'b'], ' or ')
'a or b'
>>> humanize_list(['a'])
'a'
>>> humanize_list([])
''
>>> humanize_list(['a', 'b'], wrap_item_with="`")
'`a` and `b`'
>>> humanize_list(['a', 'b', 'c', 'd'], " = ", sep=" + ")
'a + b + c = d'
"""
if not list_:
return ''
result = (['{1}{0}{1}'.format(i, wrap_item_with) for i in list_]
if wrap_item_with else list_[:])
if len(result) == 1:
return result[0]
return '{}{}{}'.format(sep.join(result[:-1]), last_sep, result[-1])
if __name__ == '__main__':
import doctest
doctest.testmod()