title: Python - Insert line break every x-number of elements
body: <p>I have looked around SO for similar questions, but it seems that this has only been asked for Visual Basic. What I want to do is rather simple, I guess, but being a beginner I can't seem to find the way to get it done.</p>
  
  <p>I have a list of strings like this:</p>
  
  <pre><code>macrolist = ['hans', 'are', 'we', 'the', 'baddies', 'cake', 'or', 'death', 'cake', 'please', 'do', 'you', 'have', 'a', 'flag']
  </code></pre>
  
  <p>The problem is that this has been generated after reading from a tabbed file which has only 5 words per line, and then doing a number of changes in it (mainly shuffling). In order to generate an output file, I can join the elements of my list thus:</p>
  
  <pre><code>'\t'.join(macrolist)
  </code></pre>
  
  <p>...and then write it to a new file, but this will output a text file with just one line of tab-spaced items. I need to get a file which has only 5 words per line, like the original, so I thought that, before joining the list, I could insert a line break every 5 words. I've tried the following, but I get an error for "unsupported operand type(s) for /: 'str' and 'int'":</p>
  
  <pre><code>for index in macrolist:
      op = index/5
      if op is int:
          macrolist.append('\n')
  
  print macrolist
  </code></pre>
  
  <p>The idea I had was that, if a given index was a multiple of 5, the code could insert a line break right afterwards. Then I could join the list and the output would be as desired. But, of course, even the design could be wrong... Any ideas?</p>
  
  <p>Thanks in advance.</p>