title: Python commacode adding and
body: <p>I'm doing an assignment where we have to write a comma code as described from the book</p>
  <p>Say you have a list value like this:</p>
  <pre><code>spam = ['apples', 'bananas', 'tofu', 'cats']
  </code></pre>
  <p>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 <code>apples, bananas, tofu, and cats</code>. But your function should be able to work with any list value passed to it.</p>
  <p>We are to write it with these details:</p>
  <ul>
  <li><p>In your program, create four lists as follows:</p>
  <pre><code>animalList1 = ['dog', 'cat', 'bird', 'tiger', 'lion', 'camel']
  animalList2 = ['elephant', 'alligator']
  animalList3 = ['horse']
  animalList4 = []
  </code></pre>
  </li>
  <li><p>When you run your program, pass each list to the <code>commaCode()</code> function and then print out the result as shown in the Example Output.</p>
  </li>
  </ul>
  <p>I'm really stuck and don't know if I'm heading in the right direction. This is my code so far:</p>
  <pre><code>animalList1 = ['dog', 'cat', 'bird', 'tiger', 'lion', 'camel']
  animalList2 = ['elephant', 'alligator']
  animalList3 = ['horse']
  animalList4 = []
  spam = [animalList1, animalList2, animalList3, animalList4]
  
  def commacode(list):
      spam[-1] = 'and ' + str(spam-[1])
  
      for i in range(len(spam)-1):
          spam[i] = str(spam[i]) + ','
          stringList = ''
  
          for i in range(len(spam)):
              stringList = stringList + str(spam[i])
              print(stringList)
  
  print(commacode(spam))
  </code></pre>