Python help needed

Kuyateh Yankz qubzen at gmail.com
Wed Aug 7 16:36:59 EDT 2019


 
#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.

def myList(aParameter): #defined 
    finalList = [] 
    for i in range(len(aParameter)-1): #targeting the last item in the list
      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.
    return finalList
spam = ['apples', 'bananas', 'tofu', 'cats']
print(myList(spam))

#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.
#Am I on the wrong path or there's a quick fix here?
https://pastebin.com/JCXisAuz



More information about the Python-list mailing list