Comma Code (project from "Automate the Boring Stuff with Python") I'm currently learning Python using the book mentioned in the title. I just finished reading about lists, tuples, functions and methods, and one of the projects following this chapter is to create a program which takes in a list as an input and outputs a corresponding string, as the following example showcases:

Input:  ['apples', 'bananas', 'tofu', 'cats']

Output: apples, bananas, tofu, and cats

This should of course work for any list. So I've come up with the following code:

def listToString(someList):
    for i in range(len(someList)):
        newString = str(someList[i])

        if i == (len(someList)-1):
            print('and ' + str(someList[i]))
        else:
            print(newString, end=', ')

someList = ['apples','bananas','tofu','cats']
listToString(someList)


I feel like I haven't used everything (for example, some methods) which were taught in the chapter to solve the problem. Is there a more efficient way to code this project?
Ludisposed's answer is already a great improvement, however it fails to take into account some cornercases:


It only works with lists of strings.
For lists of length 1, like ["foo"], it returns " and foo" (your code has the same problem).
It raises an IndexError on an empty list.


This code avoids these problems:

def list_to_string(a_list):
    if not a_list:
        return ""
    elif len(a_list) == 1:
        return str(a_list[0])
    else:
        body = ", ".join(map(str, a_list[:-1]))
        return '{0} and {1}'.format(body, a_list[-1])


This does not currently have the Oxford comma, like your code. But you can easily add it as another special case (don't add it when len(a_list) == 2 and add it otherwise). Thanks to @DavidK for pointing this out in the comments.