How to zip a list of lists in python?
<p>I have a list of lists</p>

<pre><code>sample = [['A','T','N','N'],['T', 'C', 'C', 'C']],[['A','T','T','N'],['T', 'T', 'C', 'C']].
</code></pre>

<p>I am trying to zip the file such that only A/T/G/C are in lists and the output needs to be a list</p>

<pre><code>[['AT','TCCC'],['ATT','TTCC']]
</code></pre>

<p>When I use this code:</p>

<pre><code>tt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in sample]
</code></pre>

<p>However, I only get the output as:</p>

<pre><code>['ATT','TTCC']
</code></pre>

<p>Any suggestions where I am going wrong?</p>

<p>In my actual code I am first transposing the lists:</p>

<pre><code>seq_list = [['TCCGGGGGTATC', 'TCCGTGGGTATC', ...]]  # one nested list

numofpops = len(seq_list)

### Tranposing. Moving along the columns only

#column_list = []
for k in range(len(seq_list)):
    column_list = [[] for i in range(len(seq_list[k][0]))]
    for seq in seq_list[k]:
        for i, nuc in enumerate(seq):
            column_list[i].append(nuc)
            ddd = column_list
    print ddd

tt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in ddd]
print tt
</code></pre>

<p>Your actual code is <em>discarding lists</em>. You only ever process the <em>last entry</em>.</p>

<p>Your code works fine otherwise. Just do that <em>in the loop</em> and then append the result to some final list:</p>

<pre><code>results = []

for k in range(len(seq_list)):
    column_list = [[] for i in range(len(seq_list[k][0]))]
    for seq in seq_list[k]:
        for i, nuc in enumerate(seq):
            column_list[i].append(nuc)
    # process `column_list` here, in the loop (no need to assign to ddd)
    tt = ["".join(y for y in x if y in {'A','G','T','C'}) for x in column_list]

    results.append(tt)
</code></pre>

<p>Note that you could use the <code>zip()</code> function instead of your transposition list:</p>

<pre><code>results = []
for sequence in seq_list:
    for column_list in zip(*sequence):
        tt = [''.join([y for y in x if y in 'AGTC']) for x in column_list]
        results.append(tt)
</code></pre>

python list