How to rewrite the following zip code in nested for and if loops?
<p>List l has the following elements</p>

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

<p>List l was split into pairs using tuples, such that every alphabet corresponded as A:6, G:6, C:35 ..etc
If a value for less than 10, then the alphabets were converted to Z.
The following is the code:</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>How can one code the same thing using nested for and if loops? (I need to do this exercise to help understand coding in Python better) </p>

<p>Here is my attempt:</p>

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

<p>Note 1: If someone could help me frame the question better- specific to the questions, please do suggest changes</p>

<p>The above question and code (credits: P. Cunningham) can be found <a href="http://stackoverflow.com/a/39441797/6820344">here</a></p>

<p>To reverse engineer the code, usually working on the python console is the best way to proceed. So, I would start taking the different statements apart one by one and see what each does. Now, there will probably be many different ways to explain/rewrite that code, so mine will be one that hopefully will help you understand the process.</p>

<p>Your code is good, but there is one problem and one discrepancy to the final desired output.</p>

<ol>
<li>The problem is at the first loop: you should swap the <code>for a, b</code> with the <code>for sub in l</code> line, because this should give you an error about <code>sub</code> not being defined or so.</li>
<li>The discrepancy is that <code>pairs</code> is a list of lists, and instead you are trying to append a tuple. I say "try" because the statement is invalid and should be written as <code>pairs.append(("Z",i))</code> for example. But still misses the subtle <code>list</code> which is defined in the original code. In fact there you have a <code>pairs.append([])</code>, and the full logic inside creates what is called a <code>list comprehension</code>. </li>
</ol>

<p>Nevertheless, very good work from your part.</p>

<p>The final "new code" looks like below and you can see it in action at: <a href="https://eval.in/639908" rel="nofollow">https://eval.in/639908</a></p>

<pre><code>l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
pairs = []
# for each element in the list
for sub in l:   
    # we will create a new empty list
    new_list = []

    # split 'sub' into the string and the numbers parts
    a, b = sub.split(None, 1)   

    # further, split the numbers into a list
    numbers = b.split()

    # now using positional index, we will...
    for x in range(len(a)):
        # ... create a new tuple taking one character from string 
        # 'a' and one number from list 'numbers'
        new_tuple = ( a[x], numbers[x] )

        # if the value is less than 10, then...
        if int(numbers[x]) &lt; 10:
            # ... let's just put "Z" 
            new_tuple = ("Z", numbers[x])   

        # append this to the temporary list
        new_list.append(new_tuple)    

    # once the sequence is complete, add it to the main list
    pairs.append(new_list)   

# When it's all done, print the list
print pairs
</code></pre>

python if-statement for-loop