SHARE
TWEET

Untitled

a guest Aug 7th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #trying to 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.
  2.  
  3. def myList(aParameter): #defined
  4.     finalList = []
  5.     for i in range(len(aParameter)-1): #targeting the last item in the list
  6.       finalList.append('and '+ aParameter[-1]) #Removes the last item in the list, append the last item with THE and put it back, then put it into the FINAL LIST FUNCTION.
  7.     return finalList
  8. spam = ['apples', 'bananas', 'tofu', 'cats']
  9. print(myList(spam))
  10.  
  11. #Got up to this point, still couldn't get rid of the '' around the items and the []. I tried (','.join()) but could not get there.
  12. #Am I on the wrong path or there's a quick fix here?
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top