Create multiple dictionaries from a single iterator in nested for loops
<p>I have a nested list comprehension which has created a list of six lists of ~29,000 items. I'm trying to parse this list of final data, and create six separate dictionaries from it. Right now the code is very unpythonic, I need the right statement to properly accomplish the following:</p>

<p>1.) Create six dictionaries from a single statement.</p>

<p>2.) Scale to any length list, i.e., not hardcoding a counter shown as is.</p>

<p>I've run into multiple issues, and have tried the following:</p>

<p>1.) Using while loops</p>

<p>2.) Using break statements, will break out of the inner most loop, but then does not properly create other dictionaries. Also break statements set by a binary switch.</p>

<p>3.) if, else conditions for n number of indices, indices iterate from 1-29,000, then repeat.</p>

<p>Note the ellipses designate code omitted for brevity.</p>

<pre><code># Parse csv files for samples, creating a dictionary of key, value pairs and multiple lists.
with open('genes_1') as f:
    cread_1 = list(csv.reader(f, delimiter = '\t'))
    sample_1_values = [j for i, j in (sorted([x for x in {i: float(j) 
                        for i, j in cread_1}.items()], key = lambda v: v[1]))]
    sample_1_genes = [i for i, j in (sorted([x for x in {i: float(j) 
                            for i, j in cread_1}.items()], key = lambda v: v[1]))]

... 

# Compute row means.
mean_values = []
for i, (a, b, c, d, e, f) in enumerate(zip(sample_1_values, sample_2_values, sample_3_values, sample_4_values, sample_5_values, sample_6_values)):
    mean_values.append((a + b + c + d + e + f)/6)

# Provide proper gene names for mean values and replace original data values by corresponding means.
sample_genes_list = [i for i in sample_1_genes, sample_2_genes, sample_3_genes, sample_4_genes, sample_5_genes, sample_6_genes]

sample_final_list = [sorted(zip(sg, mean_values)) for sg in sample_genes_list]

# Create multiple dictionaries from normalized values for each dataset.
class BreakIt(Exception): pass
try: 
    count = 1         
    for index, items in enumerate(sample_final_list):
        sample_1_dict_normalized = {}             
        for index, (genes, values) in enumerate(items):
            sample_1_dict_normalized[genes] = values
            count = count + 1
            if count == 29595:
                raise BreakIt
except BreakIt:
    pass
</code></pre>

<p>...</p>

<pre><code>try: 
    count = 1         
    for index, items in enumerate(sample_final_list):
        sample_6_dict_normalized = {}             
        for index, (genes, values) in enumerate(items):
            if count &gt; 147975:
                sample_6_dict_normalized[genes] = values
            count = count + 1
            if count == 177570:
                raise BreakIt
except BreakIt:
    pass

# Pull expression values to qualify overexpressed proteins.
print 'ERG values:'
print 'Sample 1:', round(sample_1_dict_normalized.get('ERG'), 3) 
print 'Sample 6:', round(sample_6_dict_normalized.get('ERG'), 3)  
</code></pre>
