Skip to content

Function to format a list with commas and a final 'and'/'or' #322

@lordmauve

Description

@lordmauve

A function that I frequently want is to print a list of strings, separating them with commas, plus a final conjunction like 'and' or 'or'. The Oxford comma should be the default but some users might like to disable it.

>>> human_list(['spam'])
'spam'
>>> human_list(['egg', 'spam'])
'egg and spam'
>>> human_list(['egg', 'bacon', 'spam'], conjunction='or')
'egg, bacon, or spam'
>>> human_list(['spam', 'spam', 'egg', 'bacon', 'spam'])
'spam, spam, egg, bacon, and spam'
>>> human_list(['lobster thermidor aux crevettes', 'garnished with truffle paté', 'with a fried egg on top', 'spam'], oxford=False)
'lobster thermidor aux crevettes, garnished with truffle paté, with a fried egg on top and spam'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions