Skip to main content
added 230 characters in body
Source Link
>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> print(', '.join(spam[:-2] + [spam[-2] + ' and ' + spam[-1]]))
apples, bananas, tofu and cats

The comma after tofu should not exist.

Steps:

  1. ', '.join(...) creates a comma-separated string of the values contained in spam
  2. spam[:-2] slices spam and creates a sublist from the first to the second-to-last item. Have a look at python's slice notation.
  3. [spam[-2] + ' and ' + spam[-1]] creates a list containing: spam's second-to-last item ('tofu'), the word 'and' and spam's last item ('cats'). Creating a list is necessary because python doesn't know how to concatenate a list + another type of object. We can only concatenates lists.

Edit: Oxford Comma is a thing, apparently. Well, hooray english.

>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> print(', '.join(spam[:-1] + [' and ' + spam[-1]]))
apples, bananas, tofu, and cats
>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> print(', '.join(spam[:-2] + [spam[-2] + ' and ' + spam[-1]]))
apples, bananas, tofu and cats

The comma after tofu should not exist.

Steps:

  1. ', '.join(...) creates a comma-separated string of the values contained in spam
  2. spam[:-2] slices spam and creates a sublist from the first to the second-to-last item. Have a look at python's slice notation.
  3. [spam[-2] + ' and ' + spam[-1]] creates a list containing: spam's second-to-last item ('tofu'), the word 'and' and spam's last item ('cats'). Creating a list is necessary because python doesn't know how to concatenate a list + another type of object. We can only concatenates lists.
>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> print(', '.join(spam[:-2] + [spam[-2] + ' and ' + spam[-1]]))
apples, bananas, tofu and cats

The comma after tofu should not exist.

Steps:

  1. ', '.join(...) creates a comma-separated string of the values contained in spam
  2. spam[:-2] slices spam and creates a sublist from the first to the second-to-last item. Have a look at python's slice notation.
  3. [spam[-2] + ' and ' + spam[-1]] creates a list containing: spam's second-to-last item ('tofu'), the word 'and' and spam's last item ('cats'). Creating a list is necessary because python doesn't know how to concatenate a list + another type of object. We can only concatenates lists.

Edit: Oxford Comma is a thing, apparently. Well, hooray english.

>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> print(', '.join(spam[:-1] + [' and ' + spam[-1]]))
apples, bananas, tofu, and cats
Source Link

>>> spam = ['apples', 'bananas', 'tofu', 'cats']
>>> print(', '.join(spam[:-2] + [spam[-2] + ' and ' + spam[-1]]))
apples, bananas, tofu and cats

The comma after tofu should not exist.

Steps:

  1. ', '.join(...) creates a comma-separated string of the values contained in spam
  2. spam[:-2] slices spam and creates a sublist from the first to the second-to-last item. Have a look at python's slice notation.
  3. [spam[-2] + ' and ' + spam[-1]] creates a list containing: spam's second-to-last item ('tofu'), the word 'and' and spam's last item ('cats'). Creating a list is necessary because python doesn't know how to concatenate a list + another type of object. We can only concatenates lists.