title: Write list items to a text file in Python
body: <p>I have a - probably - trivial question that's been bugging me for some time and I still haven't found an answer. Many of my scripts read some files, compare values from them and save these in a list or dictionary from which I then write an output file of some sort. What I always do is, I loop over the list and write the single items to my output, separated by a tab, comma, or line break. What I am always wondering is how I can prevent a separator appearing after the last item on my list has been printed.</p>
  
  <p>Here is an example:</p>
  
  <pre><code>dict1 = {a: [1,2,3], b: [4,5,6], c: [7,8,9]}
  for key in dict1:
      outfile.write(key +": ")
      for item in dict1[key]:
          outfile.write(str(item) +", ")
      outfile.write("\n")
  </code></pre>
  
  <p>The output would then look like this:</p>
  
  <pre><code>a: 1, 2, 3, 
  b: 4, 5, 6, 
  c: 7, 8, 9, 
  </code></pre>
  
  <p>How do I avoid that last ", "?</p>