How to pass a list of lists through a for loop in Python?
<p>I have a list of lists :</p>

<pre><code>sample = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']]
count = [[4,3],[4,2]]
correctionfactor  = [[1.33, 1.5],[1.33,2]]
</code></pre>

<p>I calculate frequency of each character (pi), square it and then sum (and then I calculate het = 1 - sum). </p>

<pre><code>The desired output [[1,2],[1,2]] #NOTE: This is NOT the real values of expected output. I just need the real values to be in this format. 
</code></pre>

<p>The problem: I do not how to pass the list of lists(sample, count) in this loop to extract the values needed. I previously passed only a list (eg <code>['TACT','TTTT'..]</code>) using this code. </p>

<ul>
<li>I suspect that I need to add a larger for loop, that indexes over each element in sample (i.e. indexes over <code>sample[0] = ['TTTT', 'CCCZ']</code> and <code>sample[1] = ['ATTA', 'CZZC']</code>. I am not sure how to incorporate that into the code.</li>
</ul>

<p>**
Code</p>

<pre><code>list_of_hets = []
for idx, element in enumerate(sample):
    count_dict = {}
    square_dict = {}
    for base in list(element):
         if base in count_dict:
            count_dict[base] += 1
        else:
            count_dict[base] = 1
    for allele in count_dict: #Calculate frequency of every character
        square_freq = (count_dict[allele] / count[idx])**2 #Square the frequencies
        square_dict[allele] = square_freq        
    pf = 0.0
    for i in square_dict:
        pf += square_dict[i]   # pf --&gt; pi^2 + pj^2...pn^2 #Sum the frequencies
    het = 1-pf                    
    list_of_hets.append(het)
print list_of_hets

"Failed" OUTPUT:
line 70, in &lt;module&gt;
square_freq = (count_dict[allele] / count[idx])**2
TypeError: unsupported operand type(s) for /: 'int' and 'list'er
</code></pre>

<p>I'm not completely clear on how you want to handle the 'Z' items in your data, but this code replicates the output for the sample data in <a href="https://eval.in/658468" rel="nofollow">https://eval.in/658468</a></p>

<pre><code>from __future__ import division

bases = set('ACGT')
#sample = [['TTTT', 'CCCZ'], ['ATTA', 'CZZC']]
sample = [['ATTA', 'TTGA'], ['TTCA', 'TTTA']]

list_of_hets = []
for element in sample:
    hets = []
    for seq in element:
        count_dict = {}
        for base in seq:
            if base in count_dict:
                count_dict[base] += 1
            else:
                count_dict[base] = 1
        print count_dict

        #Calculate frequency of every character
        count = sum(1 for u in seq if u in bases)
        pf = sum((base / count) ** 2 for base in count_dict.values())
        hets.append(1 - pf)
    list_of_hets.append(hets)

print list_of_hets
</code></pre>

<p><strong>output</strong></p>

<pre><code>{'A': 2, 'T': 2}
{'A': 1, 'T': 2, 'G': 1}
{'A': 1, 'C': 1, 'T': 2}
{'A': 1, 'T': 3}
[[0.5, 0.625], [0.625, 0.375]]
</code></pre>

<p>This code could be simplified further by using a collections.Counter instead of the <code>count_dict</code>.</p>

<p>BTW, if the symbol that's not in 'ACGT' is <em>always</em> 'Z' then we can speed up the <code>count</code> calculation. Get rid of <code>bases = set('ACGT')</code> and change</p>

<pre><code>count = sum(1 for u in seq if u in bases)
</code></pre>

<p>to</p>

<pre><code>count = sum(1 for u in seq if u != 'Z')
</code></pre>

python list for-loop division