How to create tuples from a single list with alpha-numeric chacters?
<p>I have the following list with 2 elements:</p>

<pre><code>['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
</code></pre>

<p>I need to make a list or zip file such that each alphabet corresponds to its number further in the list. For example in list[0] the list/zip should read</p>

<pre><code>{"A":"6", "G":"6", "C":"35","T":"25","T":"10"}
</code></pre>

<p>Can I make a list of such lists/zips that stores the corresponding vales for list[0], list[1],...list[n]?</p>

<p>Note: The alphabets can only be A,G,C or T, and the numbers can take anyvalue</p>

<p>Edit 1: Previously, I thought I could use a dictionary. But several members pointed out that this cannot be done. So I just want to make a list or zip or anything else recommended to pair the Alphabet element to its corresponding number. </p>

<p>Use tuples splitting once to get the pairs, then split the second element of each pair,  <em>zip</em> together:</p>

<pre><code>l  =['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']

pairs =  [zip(a,b.split()) for a,b in (sub.split(None,1) for sub in l]
</code></pre>

<p>Which would give you:</p>

<pre><code>[[('A', '6'), ('G', '6'), ('C', '35'), ('T', '25'), ('T', '10')], [('A', '7'), ('G', '7'), ('G', '28'), ('G', '29'), ('T', '2')]]
</code></pre>

<p>Of using a for loop with <em>list.append:</em></p>

<pre><code>l  = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
out = []
for a,b in (sub.split(None,1) for sub in l ):
    out.append(zip(a,b))
</code></pre>

<p>If you want to convert any letter to <code>Z</code> where the digit is &lt; 10, you just need another loop where we check the digit in each pairing:</p>

<pre><code>pairs = [[("Z", i ) if int(i) &lt; 10 else (c, i) for c,i in zip(a, b.split())] 
         for a,b in (sub.split(None, 1) for sub in l)]
print(pairs)
</code></pre>

<p>Which would give you:</p>

<pre><code>[[('Z', '6'), ('Z', '6'), ('C', '35'), ('T', '25'), ('T', '10')], [('Z', '7'), ('Z', '7'), ('G', '28'), ('G', '29'), ('Z', '2')]]
</code></pre>

<p>To  break it into a regular loop:</p>

<pre><code>pairs = []
for a, b in (sub.split(None, 1) for sub in l):
    pairs.append([("Z", i) if int(i) &lt; 10 else (c, i) for c, i in zip(a, b.split())])
print(pairs)
</code></pre>

<p><code>[("Z", i) if int(i) &lt; 10 else (c, i) for c, i in zip(a, b.split())]</code> sets the letter to <code>Z</code> if the corresponding digit <code>i</code> is <code>&lt; 10</code> or else we just leave the letter as is.</p>

<p>if you want to get back to the original pairs after you just need to <a href="https://en.wikipedia.org/wiki/Transpose" rel="nofollow">transpose</a> with <em>zip</em>:</p>

<pre><code>In [13]: l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']

In [14]: pairs = [[("Z", i) if int(i) &lt; 10 else (c, i) for c, i in zip(a, b.split())] for a, b in
   ....:          (sub.split(None, 1) for sub in l)]

In [15]: pairs
Out[15]: 
[[('Z', '6'), ('Z', '6'), ('C', '35'), ('T', '25'), ('T', '10')],
 [('Z', '7'), ('Z', '7'), ('G', '28'), ('G', '29'), ('Z', '2')]]

In [16]: unzipped = [["".join(a), " ".join(b)] for a, b in (zip(*tup) for tup in pairs)]

In [17]: unzipped
Out[17]: [['ZZCTT', '6 6 35 25 10'], ['ZZGGZ', '7 7 28 29 2']]
</code></pre>

<p><code>zip(*...)</code> will give you the original elements back into a tuple of their own, we then just need to join the strings back together. If you wanted to get back to the total original state you could just join again:</p>

<pre><code>In[18][ " ".join(["".join(a), " ".join(b)]) for a, b in (zip(*tup) for tup in pairs) ]
Out[19]: ['ZZCTT 6 6 35 25 10', 'ZZGGZ 7 7 28 29 2']
</code></pre>

python list dictionary zip tuples