title: Python Unordered Listwith nodes: __str__ method
body: <p>I am trying to write an __str__method for my UnorderedList class that can allow the list to be printed out in a format like: [1,2,3,4,5].</p>
  
  <p>But for some reason it is only printing out the first position of the list. eg. [1] instead of [1,2,3,4,5]    </p>
  
  <pre><code>def __str__(self):
  
      result = "["
      node = self.head
      if node != None:
          result += str(node.data)
          node = node.next
          for i in range(self.num_elements - 1):
              result += ", " + str(node.data)
              node = node.next
      result += "]"
      return result    
  </code></pre>
  
  <p>Here is the rest of my Node and UnorderedList class methods...</p>
  
  <pre><code>class Node:
  def __init__(self,initdata):
      self.data = initdata
      self.next = None
  
  def getData(self):
      return self.data
  
  def getNext(self):
      return self.next
  
  def setData(self,newdata):
      self.data = newdata
  
  def setNext(self,newnext):
      self.next = newnext
  
  
  
  class UnorderedList:
  
  def __init__(self):
      self.head = None
  
  def isEmpty(self):
      return self.head == None
  
  def add(self,item):
      temp = Node(item)
      temp.setNext(self.head)
      self.head = temp
  
  def size(self):
      current = self.head
      count = 0
      while current != None:
          count = count + 1
          current = current.getNext()
      return count
  
  def search(self,item):
      current = self.head
      found = False
      while current != None and not found:
          if current.getData() == item:
              found = True
          else:
              current = current.getNext()
      return found
  
  def remove(self,item):
      current = self.head
      previous = None
      found = False
      while not found:
          if current.getData() == item:
              found = True
          else:
              previous = current
              current = current.getNext()
  
      if previous == None:
          self.head = current.getNext()
      else:
          previous.setNext(current.getNext())
  
  def __str__(self):
  
      result = "["
      node = self.head
      if node != None:
          result += str(node.data)
          node = node.next
          for i in range(0 - 1):
              result += ", " + str(node.data)
              node = node.next
      result += "]"
      return result
  </code></pre>
  
  <p>Any help would be awesome. Also I feel like there is a much easier way of writing the <strong>str</strong>.</p>