Skip to content

Instantly share code, notes, and snippets.

@Gwith
Created August 7, 2016 17:13
Show Gist options
  • Save Gwith/0d0122ac0583418b08a548ecd253b9f5 to your computer and use it in GitHub Desktop.
Save Gwith/0d0122ac0583418b08a548ecd253b9f5 to your computer and use it in GitHub Desktop.
'''
Goal:
Say you have a list value like this:
spam = ['apples', 'bananas', 'tofu', 'cats']
Write a function that takes a list value as an argument and returns a string with all the items separated by a comma
and a space, with and inserted before the last item. For example, passing the previous spam list to the function would
return 'apples, bananas, tofu, and cats'. But your function should be able to work with any list value passed to it.
'''
'''
Methods Tried:
--------------------------------------------
spam = ['apples', 'bananas', 'tofu', 'cats']
def list_function(x):
print(','.join(spam))
list_function(spam)
--------------------------------------------
--------------------------------------------
spam = ['apples', 'bananas', 'tofu', 'cats']
def list_function(x):
print(str(spam[0:2]) + ' and ' + str(spam[3]))
list_function(spam)
--------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment