How to fix python program that appears to be doing an extra loop?
<p>A portion of a python program I am writing seems to be looping an extra time. The part of the program that isn't working is below. It is supposed to ask for a string from the user and create a two-dimensional list where each distinct character of the string is put in its own sub-list. (Hopefully that makes sense... if not I can try to explain better. Perhaps the code will help)</p>

<pre><code>def getInput(emptyList):
    inputString = input("Please enter a sentence:\n").strip().upper()
    functionList = [x for x in inputString]
    emptyList.extend(functionList)
    return 0

def sortList(listA,listB):
    listA.sort()
    currentElement = listA[0]
    compareTo = listA[0]
    elementsCounted = 0
    i = 0
    listB.append([])
    while elementsCounted  &lt; len(listA):
        while currentElement == compareTo:
            listB[i].append(currentElement)
            elementsCounted += 1
            print(listB)
            if elementsCounted &lt; len(listA):
                currentElement = listA[elementsCounted]
            else:
                break
        if currentElement != compareTo:
            i += 1
            listB.append([])
            compareTo = listA[i]

    return 0

def main():
    myList = list()
    sortedList = list()
    getInput(myList)
    sortList(myList,sortedList)
    print(sortedList)

main()
</code></pre>

<p>If the user enters <code>qwerty</code>, the program returns <code>[['E'], ['Q'], ['R'], ['T'], ['W'], ['Y']]</code> which is correct but if the user enters <code>qwwerrty</code> the program returns <code>[['E'], ['Q'], ['R', 'R'], [], ['T'], ['W', 'W'], [], ['Y']]</code>. Note the extra empty list after each "double" character. It appears that the loop is making one extra iteration or that the <code>if</code> statement before <code>listB.append([])</code> isn't written properly.</p>

<p>I can't seem to figure it out more than this. Thank you in advance for your help.</p>

<p>NOTE: <code>elementsCounted</code> should be a cumulative tally of each element that has been processed from listA. <code>i</code> is the index of the current element in listB. For example, if <code>['A','A','B']</code> was listA and the program is processing the second A, then it is the second element being counted but <code>i</code> is still 0 because it belongs in listB[0]. <code>currentElement</code> is the one currently being processed and it is being compared to the first element that was processed as that "i". For the <code>['A','A','B'] example, when processing the second A, it is being compared to the first A to see if</code>i<code>should be incremented. In the next loop, it is comparing 'B' to the first 'A' and thus will increase</code>i` by one since 'B' belongs in the next sub-list.</p>

<p>Your mistake lies in this part:</p>

<pre><code>if currentElement != compareTo:
    ...
    compareTo = listA[i]
</code></pre>

<p>It should be:</p>

<pre><code>if currentElement != compareTo:
    ...
    compareTo = listA[elementsCounted]
</code></pre>

<p>It's an overly complex function for such a simple task.</p>

python list loops if-statement while-loop