How to make a list of lists in Python when it has multiple separators?
<p>The sample file looks like this (all on one line, wrapped for legibility):</p>

<pre><code> ['&gt;1\n', 'TCCGGGGGTATC\n', '&gt;2\n', 'TCCGTGGGTATC\n',
  '&gt;3\n', 'TCCGTGGGTATC\n', '&gt;4\n', 'TCCGGGGGTATC\n',
  '&gt;5\n', 'TCCGTGGGTATC\n', '&gt;6\n', 'TCCGTGGGTATC\n',
  '&gt;7\n', 'TCCGTGGGTATC\n', '&gt;8\n', 'TCCGGGGGTATC\n','\n',
  '$$$\n', '\n',
  '&gt;B1\n', 'ATCGGGGGTATT\n', '&gt;B2\n', 'TT-GTGGGAATC\n',
  '&gt;3\n', 'TTCGTGGGAATC\n', '&gt;B4\n', 'TT-GTGGGTATC\n',
  '&gt;B5\n', 'TTCGTGGGTATT\n', '&gt;B6\n','TTCGGGGGTATC\n',
  '&gt;B7\n', 'TT-GTGGGTATC\n', '&gt;B8\n', 'TTCGGGGGAATC\n',
  '&gt;B9\n', 'TTCGGGGGTATC\n','&gt;B10\n', 'TTCGGGGGTATC\n',
  '&gt;B42\n', 'TT-GTGGGTATC\n']
</code></pre>

<p>The <code>$$$</code>  separates the two sets. I need to use <code>.strip</code> function and remove the <code>\n</code> and all the "headers". </p>

<p>I need to make a list of lists (as below) and replace "-" with Z (again, all on one line; wrapped here for legibility):</p>

<pre><code>  [['TCCGGGGGTATC','TCCGTGGGTATC','TCCGTGGGTATC', 'TCCGGGGGTATC',
    'TCCGTGGGTATC',CGTGGGTATC','TCCGTGGGTATC', 'TCCGGGGGTATC'],
   ['ATCGGGGGTATT', 'TT-GTGGGAATC','TTCGTGGGAATC', 'TT-GTGGGTATC',
    'TTCGTGGGTATT', 'TTCGGGGGTATC','TT-GTGGGTATC', 'TTCGGGGGAATC',
    'TTCGGGGGTATC', 'TTCGGGGGTATC','TT-GTGGGTATC]]
</code></pre>

<p>You can exploit the smaller length of the headers (and other unwanted items) as the criterion to filter them out. You start by creating a list containing one list and <em>appending</em> the items that pass the length test to the inner list. </p>

<p>A new sublist is added to the resulting list when the <em>separator</em> <code>'$$$'</code> is reached, and the length test is again used to add the remaining items to this new sublist:</p>

<pre><code>lst = ['&gt;1\n', 'TCCGGGGGTATC\n', '&gt;2\n', 'TCCGTGGGTATC\n', '&gt;3\n', 'TCCGTGGGTATC\n', '&gt;4\n', 'TCCGGGGGTATC\n', '&gt;5\n', 'TCCGTGGGTATC\n', '&gt;6\n', 'TCCGTGGGTATC\n', '&gt;7\n', 'TCCGTGGGTATC\n', '&gt;8\n', 'TCCGGGGGTATC\n','\n', '$$$\n', '\n', '&gt;B1\n', 'ATCGGGGGTATT\n', '&gt;B2\n', 'TT-GTGGGAATC\n', '&gt;3\n', 'TTCGTGGGAATC\n', '&gt;B4\n', 'TT-GTGGGTATC\n', '&gt;B5\n', 'TTCGTGGGTATT\n', '&gt;B6\n','TTCGGGGGTATC\n', '&gt;B7\n', 'TT-GTGGGTATC\n', '&gt;B8\n', 'TTCGGGGGAATC\n', '&gt;B9\n', 'TTCGGGGGTATC\n','&gt;B10\n', 'TTCGGGGGTATC\n','&gt;B42\n', 'TT-GTGGGTATC\n']

result = [[]]
for x in lst:
    if len(x) &gt; 6:
        result[-1].append(x.strip())
    if x.startswith('$$$'):
        result.append([])
print(result)
# [['TCCGGGGGTATC', 'TCCGTGGGTATC', 'TCCGTGGGTATC', 'TCCGGGGGTATC', 'TCCGTGGGTATC', 'TCCGTGGGTATC', 'TCCGTGGGTATC', 'TCCGGGGGTATC'], ['ATCGGGGGTATT', 'TT-GTGGGAATC', 'TTCGTGGGAATC', 'TT-GTGGGTATC', 'TTCGTGGGTATT', 'TTCGGGGGTATC', 'TT-GTGGGTATC', 'TTCGGGGGAATC', 'TTCGGGGGTATC', 'TTCGGGGGTATC', 'TT-GTGGGTATC']]
</code></pre>

python list readfile strip